﻿using AutoMapper;
using Dapper;
using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Office2013.Word;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Hangfire;
using Hangfire.LiteDB;
using housingsafety.server.Caching.Core;
using housingsafety.server.Entities;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Models;
using housingsafety.server.Repository;
using housingsafety.server.Repository.Extensions;
using housingsafety.server.Svcs;
using KS3;
using KS3.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Minio.DataModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Npgsql;
using NPOI.HPSF;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using Quartz.Util;
using RestSharp.Extensions;
using SelfBuiltHouseSafety.Models;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// 房屋管理服务
    /// </summary>
    public class HouseMngController : BaseController<HouseMngController>
    {
        public IConfiguration Configuration { get; }
        private readonly ICache _cache;
        private readonly IMapper _mapper;
        private readonly IStorageProvider _storageProvider;
        private readonly ZmDbContext _dbContext;
        private readonly IRepository<ZmUser> _userRepository;
        private readonly IRepository<ZmBuildings> _buildingRepository;
        private readonly IRepository<ZmFileAttach> _fileAttachRepository;
        private readonly IOptions<StaticDataConf> _staticVtOptions;
        private readonly IRepository<ZmDepartment> _departmentRepository;
        private readonly IRepository<ZmPcYhLogDx> _zmPcYhLogDxRepository;
        private readonly IRepository<ZmPcYhLog> _zmPcYhLogRepository;
        private readonly IRepository<ZmPcYh> _zmPcYhRepository;
        private readonly IRepository<ZmPcBase> _zmPcBaseRepository;
        private readonly IRepository<ZmPcBaseLog> _zmPcBaseLogRepository;

        private readonly IDbTransaction _transaction;

        private readonly Microsoft.AspNetCore.Http.Features.FormOptions _defaultFormOptions = new Microsoft.AspNetCore.Http.Features.FormOptions();
        private readonly string[] _permittedExtensions = { ".mp3", ".mp4", ".jpg", ".jpeg", ".png", ".geojson", ".doc", ".docx" };

        private static object _lock = new object();

        private readonly KS3Client _kS3Client;


        public HouseMngController(IStorageProvider storageProvider
            , KS3Client kS3Client
            , IRepository<ZmPcYh> zmPcYhRepository
            , IRepository<ZmPcYhLog> zmPcYhLogRepository
            , IRepository<ZmPcYhLogDx> zmPcYhLogDxRepository
            , IRepository<ZmDepartment> departmentRepository
            , IRepository<ZmUser> userRepository
            , IRepository<ZmFileAttach> fileAttachRepository
            , IMapper mapper, ICacheProvider cacheProvider, ZmDbContext dbContext
            , IOptions<StaticDataConf> staticVtOptions
            , ILogger<HouseMngController> logger,
            IConfiguration configuration
            , IRepository<ZmSystemLog> systemLogRepository, IRepository<ZmBuildings> buildingRepository, IRepository<ZmPcBase> zmPcBaseRepository, IRepository<ZmPcBaseLog> zmPcBaseLogRepository) : base(logger, systemLogRepository)
        {
            _kS3Client = kS3Client;
            _zmPcYhRepository = zmPcYhRepository;
            _zmPcYhLogRepository = zmPcYhLogRepository;
            _zmPcYhLogDxRepository = zmPcYhLogDxRepository;
            _zmPcBaseLogRepository = zmPcBaseLogRepository;
            _zmPcBaseRepository = zmPcBaseRepository;
            _buildingRepository = buildingRepository;
            _storageProvider = storageProvider;
            _mapper = mapper;
            _dbContext = dbContext;
            _cache = cacheProvider.CreateCache();
            _userRepository = userRepository;
            _fileAttachRepository = fileAttachRepository;
            _staticVtOptions = staticVtOptions;
            _departmentRepository = departmentRepository;
            Configuration = configuration;

        }

        /// <summary>
        ///根据用户编号获取用户账号信息
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-attr")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmUserdto2>), 200)]
        public RespRet<ZmUserdto2> GetBuildingsByFwbh()
        {
            try
            {
                var userNumber = this.CurrentUser();
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZmUserdto2>(false, "user not logined");
                }
                var user = _dbContext.ZmUser.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1 && u.Disabled != 1);
                if (null == user)
                {
                    var res = new RespRet<ZmUserdto2>(false, "账号不存在");
                    WriteSystemLog("housescj/search-attr", res, userNumber);
                    return res;
                }
                var a = JsonRegion(user.District);
                if (a == null)
                {
                    var res = new RespRet<ZmUserdto2>(false, "未有管辖区，请确认账号信息");
                    WriteSystemLog("housescj/search-attr", res, userNumber);
                    return res;
                }
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                var departments = _cache.Get("departments") as List<ZmDepartment>;
                if (null == departments)
                {
                    departments = _departmentRepository.Query().ToList();
                    _cache.Set("departments", departments);
                }
                var len = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Length - 3 : RegionCode.Length;
                var data = new ZmUserdto2
                {
                    NickName = user.Nickname,
                    Uname = user.Uname,
                    UnitName=user.Unitname
                };
                data.Roletype = user.RoleType;
                data.City = "北京市";
                data.Citydm = "110000";
                if (len == 6)
                {
                    var find3 = departments.Find(_ => _.RegionCode == RegionCode.Substring(0, Math.Min(6, RegionCode.Length)));
                    data.Xzq = find3?.RegionName;
                    data.Xzqdm = find3?.RegionCode;
                }
                if (len == 9)
                {
                    var find3 = departments.Find(_ => _.RegionCode == RegionCode.Substring(0, Math.Min(6, RegionCode.Length)));
                    data.Xzq = find3?.RegionName;
                    data.Xzqdm = find3?.RegionCode;
                    var find4 = departments.Find(_ => _.RegionCode == (RegionCode.Substring(0, Math.Min(9, RegionCode.Length)) + "000"));
                    data.Town = find4?.RegionName;
                    data.Towndm = find4?.RegionCode;
                }
                if (len == 12)
                {
                    var find3 = departments.Find(_ => _.RegionCode == RegionCode.Substring(0, Math.Min(6, RegionCode.Length)));
                    data.Xzq = find3?.RegionName;
                    data.Xzqdm = find3?.RegionCode;
                    var find4 = departments.Find(_ => _.RegionCode == (RegionCode.Substring(0, Math.Min(9, RegionCode.Length)) + "000"));
                    data.Town = find4?.RegionName;
                    data.Towndm = find4?.RegionCode;
                    var find5 = departments.Find(_ => _.RegionCode == RegionCode);
                    data.Village = find5?.RegionName;
                    data.Villagedm = find5?.RegionCode;
                }
                var r = new RespRet<ZmUserdto2>(true, "Ok", data);
                WriteSystemLog("housescj/search-attr", r, userNumber);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZmUserdto2>(false, ex.ToString());
                WriteSystemLogError("housescj/search-attr", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }



       


        /// <summary>
        ///根据房屋编号查找房屋基本信息
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-attrbyfwbh")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmBuildingsDto>), 200)]
        public RespRet<ZmBuildingsDto> GetBuildingsByFwbhcj([FromQuery] string fwbh)
        {
            try
            {
                var userNumber = this.CurrentUser();
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZmBuildingsDto>(false, "user not logined");
                }
                if (string.IsNullOrEmpty(fwbh))
                {
                    return new RespRet<ZmBuildingsDto>(false, "房屋编号不能为空");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                sbSql.Append($@"select  * FROM zm_buildings a   ");
                sbSqlWhere.Append($@" a.Fwbh = @Fwbh and");
                sqlParams.Add(new NpgsqlParameter("Fwbh", $"{fwbh}"));
                if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                {
                    sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                }
                var count = _dbContext.ZmBuildingsDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
                if (count < 1)
                {
                }
                var datas = _dbContext.ZmBuildingsDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (datas == null)
                {
                    var res = new RespRet<ZmBuildingsDto>(false, "未找到房屋信息");
                    WriteSystemLog("housescj/search-attr", res, userNumber);
                    return res;
                }
                var departments = _cache.Get("departments") as List<ZmDepartment>;
                if (null == departments)
                {
                    departments = _departmentRepository.Query().ToList();
                    _cache.Set("departments", departments);
                }
                var find3 = departments.Find(_ => _.RegionCode == datas.Xzqdm);
                datas.XzqdmAlias = find3?.RegionName;
                var find4 = departments.Find(_ => _.RegionCode == datas.Towndm);
                datas.TownAlias = find4?.RegionName;
                var find5 = departments.Find(_ => _.RegionCode == datas.Villagedm);
                datas.VillageAlias = find5?.RegionName;
                var r = new RespRet<ZmBuildingsDto>(true, "Ok", datas);
                WriteSystemLog("housescj/search-attr", r, userNumber);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZmBuildingsDto>(false, ex.ToString());
                WriteSystemLogError("housescj/search-attr", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 根据房屋编号获取房屋采集信息
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-attrpc")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcBaseCj>), 200)]
        public new RespRet<object> SearchAttrList2([FromQuery] string fwbh)
        {
            var userNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<object>(false, "user not logined");
                }
                if (string.IsNullOrEmpty(fwbh))
                {
                    return new RespRet<object>(false, "房屋编号不能为空");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                sbSql.Append($@"select *  FROM zm_pc_base_cj    ");
                sbSqlWhere.Append($@" Fwbh = @Fwbh and");
                sbSqlWhere.Append($@"  fwbh in (select fwbh  FROM zm_pc_baselog_cj  where Fwbh =@Fwbh  and fwzt is null) and ");
                sqlParams.Add(new NpgsqlParameter("Fwbh", $"{fwbh}"));

                if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                {
                    sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                }
                var count = _dbContext.ZmPcBaseCj.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();

                var isdb = "";
                var zclog = _dbContext.ZmPcBase.Where(c => c.Fwbh == fwbh).FirstOrDefault();
                if (zclog != null)
                {

                    if (zclog != null)
                    {
                        isdb = zclog.Jsrysstd;
                    }
                    //if ((zclog.Cs <= 1 && zclog.Jzscxyaqcksl >= 2) || (zclog.Cs >= 2 && zclog.Jzscxyaqcksl >= 2 && zclog.Jzxyssltsl >= 2))
                    //{
                    //    isdb = "已完成";
                    //}
                    //else
                    //{
                    //    isdb = "未完成";
                    //}
                }
                var datas = _dbContext.ZmPcBaseCj.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
             
                if (datas == null)
                {
                    //此角色人员未进行核验，或核验状态均为不出租和已拆除，需要把状态给到自查
                    var basecj = _dbContext.ZmPcBaseCj.Where(c => c.Fwbh == fwbh).FirstOrDefault();

                    sbSql = new StringBuilder();
                    sbSql.Append($@" select *  FROM zm_pc_base where Fwbh = @Fwbh  ");
                    var zmpcbase = _dbContext.ZmPcBase.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                    if (zmpcbase == null)
                    {
                        var Building = _dbContext.ZmBuildings.FirstOrDefault(c => c.Fwbh == fwbh);
                        if (Building == null)
                        {
                            var res = new RespRet<object>(false, "未找到房屋信息", new { data = datas, Jsrysstd = isdb });
                            WriteSystemLog("housescj/search-attrpc", res, fwbh);
                            return res;
                        }
                        datas = _mapper.Map<ZmPcBaseCj>(Building);
                    }
                    else
                    {
                        zmpcbase.Jsrysstd = zmpcbase.Tdsfdb;
                        datas = _mapper.Map<ZmPcBaseCj>(zmpcbase);
                    }
                    if (basecj != null)
                    {
                        datas.Fwzt = basecj.Fwzt;
                    }
                }
                datas.SfmzstdPhoto = null;
                datas.SfczjxgbczzrPhoto = null;
                datas.SstdsyyrkrclzxPhoto = null;
                datas.GryssdcsszblyyqksddpPhoto = null;
                datas.FwyfwdddzxccdsssfayqjshsfPhoto = null;
                var r = new RespRet<object>(true, "Ok", new { data = datas, Jsrysstd = isdb });
                WriteSystemLog("housescj/search-attrpc", r, fwbh);
                return r;
            }
            catch (Exception ex)
            {
                WriteSystemLogError("housescj/search-attrpc", new RespRet<ZmPcBaseCj>(false, ex.ToString()), fwbh);
                return new RespRet<object>(false, "系统异常");
            }
        }



        /// <summary>
        /// 根据自查记录详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-yhlogdetaile")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Yhlogdetaile([FromQuery] string fwbh)
        {
            var userNumber = this.CurrentUser();
            try
            {

                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<object>(false, "user not logined");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append($@"select *  FROM zm_pc_yh    ");

                sbSqlWhere.Append($@" fwbh = @fwbh and");
                sqlParams.Add(new NpgsqlParameter("fwbh", $"{fwbh}"));

                if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                {
                    sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                }
                var from = _dbContext.ZmPcYh.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (from == null)
                {

                    var res = new RespRet<object>(false, "未找到自查记录", from);
                    WriteSystemLog("housescj/search-yhlogdetaile", res, fwbh);
                    return res;
                }
                from.UseElectricBicyclePhoto = from.UseElectricBicyclePhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicyclePhoto));
                from.UseBottledLiquefiedGasPhoto = from.UseBottledLiquefiedGasPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasPhoto));
                from.UseStackedItemsPhoto = from.UseStackedItemsPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsPhoto));
                from.SlljdxPhoto = from.SlljdxPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxPhoto));


                from.YgbjwfwhyxPhoto = from.YgbjwfwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxPhoto));
                from.MhqwbcwhyxPhoto = from.MhqwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxPhoto));
                from.JyplmhxtwbcwhyxPhoto = from.JyplmhxtwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxPhoto));
                from.UseElectricBicycleZgPhoto = from.UseElectricBicycleZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicycleZgPhoto));
                from.UseBottledLiquefiedGasZgPhoto = from.UseBottledLiquefiedGasZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasZgPhoto));
                from.UseStackedItemsZgPhoto = from.UseStackedItemsZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsZgPhoto));
                from.SlljdxZgPhoto = from.SlljdxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxZgPhoto));
                from.YgbjwfwhyxMsgZgPhoto = from.YgbjwfwhyxMsgZgPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxMsgZgPhoto));
                from.MhqwbcwhyxZgPhoto = from.MhqwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxZgPhoto));
                from.JyplmhxtwbcwhyxZgPhoto = from.JyplmhxtwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxZgPhoto));
                //返回采集信息
                var ZmPcBase = _dbContext.ZmPcBase.FirstOrDefault(u => u.Fwbh == from.Fwbh && u.IsDelete != 1);

                var r = new RespRet<object>(true, "Ok", new { ZmPcBase = ZmPcBase, ZmPcYh = from });
                WriteSystemLog("housescj/search-yhlogdetaile", r, userNumber);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhlogdetaile", res, fwbh);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 根据自查记录详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("housescj/search-yhlogdetaileall")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> YhlogdetaileAll([FromQuery] string id)
        {
            // var userNumber = this.CurrentUser();
            try
            {

                //if (string.IsNullOrEmpty(userNumber))
                //{
                //    return new RespRet<object>(false, "user not logined");
                //}
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append($@"select *  FROM zm_pc_yhlog_cj    ");

                sbSqlWhere.Append($@" id = @fwbh and");
                sqlParams.Add(new NpgsqlParameter("fwbh", $"{id}"));

                if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                {
                    sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                }
                var from = _dbContext.ZmPcYhLogCj.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (from == null)
                {
                    sbSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append($@"select *  FROM zm_pc_yhlog    ");

                    sbSqlWhere.Append($@" id = @fwbh and");
                    sqlParams.Add(new NpgsqlParameter("fwbh", $"{id}"));

                    if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                    {
                        sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                    }
                    var yhlog = _dbContext.ZmPcYhLog.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();

                    if (yhlog == null)
                    {
                        var res = new RespRet<object>(false, "未找到记录", yhlog);
                        WriteSystemLog("housescj/search-yhlogdetaileall", res, id);
                        return res;
                    }
                    yhlog.UseElectricBicyclePhoto = yhlog.UseElectricBicyclePhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseElectricBicyclePhoto));
                    yhlog.UseBottledLiquefiedGasPhoto = yhlog.UseBottledLiquefiedGasPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseBottledLiquefiedGasPhoto));
                    yhlog.UseStackedItemsPhoto = yhlog.UseStackedItemsPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseStackedItemsPhoto));
                    yhlog.SlljdxPhoto = yhlog.SlljdxPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.SlljdxPhoto));
                    yhlog.YgbjwfwhyxPhoto = yhlog.YgbjwfwhyxPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.YgbjwfwhyxPhoto));
                    yhlog.MhqwbcwhyxPhoto = yhlog.MhqwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.MhqwbcwhyxPhoto));
                    yhlog.JyplmhxtwbcwhyxPhoto = yhlog.JyplmhxtwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.JyplmhxtwbcwhyxPhoto));
                    yhlog.UseElectricBicycleZgPhoto = yhlog.UseElectricBicycleZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseElectricBicycleZgPhoto));
                    yhlog.UseBottledLiquefiedGasZgPhoto = yhlog.UseBottledLiquefiedGasZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseBottledLiquefiedGasZgPhoto));
                    yhlog.UseStackedItemsZgPhoto = yhlog.UseStackedItemsZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.UseStackedItemsZgPhoto));
                    yhlog.SlljdxZgPhoto = yhlog.SlljdxZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.SlljdxZgPhoto));
                    yhlog.YgbjwfwhyxMsgZgPhoto = yhlog.YgbjwfwhyxMsgZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.YgbjwfwhyxMsgZgPhoto));
                    yhlog.MhqwbcwhyxZgPhoto = yhlog.MhqwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.MhqwbcwhyxZgPhoto));
                    yhlog.JyplmhxtwbcwhyxZgPhoto = yhlog.JyplmhxtwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(yhlog.JyplmhxtwbcwhyxZgPhoto));
                    //返回采集信息
                    var ZmPcBase = _dbContext.ZmPcBaselog.FirstOrDefault(u => u.Id == yhlog.BaseId && u.IsDelete != 1);

                    var re = new RespRet<object>(true, "Ok", new { ZmPcBase = ZmPcBase, ZmPcYh = yhlog });
                    WriteSystemLog("housescj/search-yhlogdetaileall", re, null);
                    return re;

                }
                from.UseElectricBicyclePhoto = from.UseElectricBicyclePhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicyclePhoto));
                from.UseBottledLiquefiedGasPhoto = from.UseBottledLiquefiedGasPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasPhoto));
                from.UseStackedItemsPhoto = from.UseStackedItemsPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsPhoto));
                from.SlljdxPhoto = from.SlljdxPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxPhoto));


                from.YgbjwfwhyxPhoto = from.YgbjwfwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxPhoto));
                from.MhqwbcwhyxPhoto = from.MhqwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxPhoto));
                from.JyplmhxtwbcwhyxPhoto = from.JyplmhxtwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxPhoto));
                from.UseElectricBicycleZgPhoto = from.UseElectricBicycleZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicycleZgPhoto));
                from.UseBottledLiquefiedGasZgPhoto = from.UseBottledLiquefiedGasZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasZgPhoto));
                from.UseStackedItemsZgPhoto = from.UseStackedItemsZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsZgPhoto));
                from.SlljdxZgPhoto = from.SlljdxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxZgPhoto));
                from.YgbjwfwhyxMsgZgPhoto = from.YgbjwfwhyxMsgZgPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxMsgZgPhoto));
                from.MhqwbcwhyxZgPhoto = from.MhqwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxZgPhoto));
                from.JyplmhxtwbcwhyxZgPhoto = from.JyplmhxtwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxZgPhoto));
                //返回采集信息
                var ZmPcBasecj = _dbContext.ZmPcBaselogCj.FirstOrDefault(u => u.Id == from.BaseId && u.IsDelete != 1);
                ZmPcBasecj.SfmzstdPhoto = (ZmPcBasecj.SfmzstdPhoto == null || ZmPcBasecj.SfmzstdPhoto == "")? null : string.Join("|", JsonStr2List(ZmPcBasecj.SfmzstdPhoto));

                ZmPcBasecj.SfczjxgbczzrPhoto = (ZmPcBasecj.SfczjxgbczzrPhoto == null || ZmPcBasecj.SfczjxgbczzrPhoto == "") ? null : string.Join("|", JsonStr2List(ZmPcBasecj.SfczjxgbczzrPhoto));

                ZmPcBasecj.SstdsyyrkrclzxPhoto= (ZmPcBasecj.SstdsyyrkrclzxPhoto == null || ZmPcBasecj.SstdsyyrkrclzxPhoto == "")  ? null : string.Join("|", JsonStr2List(ZmPcBasecj.SstdsyyrkrclzxPhoto));

                ZmPcBasecj.GryssdcsszblyyqksddpPhoto = (ZmPcBasecj.GryssdcsszblyyqksddpPhoto == null || ZmPcBasecj.GryssdcsszblyyqksddpPhoto == "") ? null : string.Join("|", JsonStr2List(ZmPcBasecj.GryssdcsszblyyqksddpPhoto));

                ZmPcBasecj.FwyfwdddzxccdsssfayqjshsfPhoto = (ZmPcBasecj.FwyfwdddzxccdsssfayqjshsfPhoto == null || ZmPcBasecj.FwyfwdddzxccdsssfayqjshsfPhoto == "") ? null : string.Join("|", JsonStr2List(ZmPcBasecj.FwyfwdddzxccdsssfayqjshsfPhoto));


                var r = new RespRet<object>(true, "Ok", new { ZmPcBase = ZmPcBasecj, ZmPcYh = from });
                WriteSystemLog("housescj/search-yhlogdetaileall", r, null);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhlogdetaileall", res, id);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 根据id获取检查记录详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-cjyhlogdetaile")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> YhlogdetaileCj([FromQuery] string id)
        {
            var userNumber = this.CurrentUser();
            try
            {

                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<object>(false, "user not logined");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append($@"select *  FROM zm_pc_yh_cj    ");

                sbSqlWhere.Append($@" id = @fwbh and");
                sqlParams.Add(new NpgsqlParameter("fwbh", $"{id}"));

                if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
                {
                    sbSql.Append($@" where {sbSqlWhere} 1=1 ");
                }
                var from = _dbContext.ZmPcYhCj.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (from == null)
                {
                    var res = new RespRet<object>(false, "未找到自查记录，请传递正确房屋编号", from);
                    WriteSystemLog("housescj/search-yhlogdetaile", res, id);
                    return res;
                }
                from.UseElectricBicyclePhoto = from.UseElectricBicyclePhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicyclePhoto));
                from.UseBottledLiquefiedGasPhoto = from.UseBottledLiquefiedGasPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasPhoto));
                from.UseStackedItemsPhoto = from.UseStackedItemsPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsPhoto));
                from.SlljdxPhoto = from.SlljdxPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxPhoto));
                from.YgbjwfwhyxPhoto = from.YgbjwfwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxPhoto));
                from.MhqwbcwhyxPhoto = from.MhqwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxPhoto));
                from.JyplmhxtwbcwhyxPhoto = from.JyplmhxtwbcwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxPhoto));
                from.UseElectricBicycleZgPhoto = from.UseElectricBicycleZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseElectricBicycleZgPhoto));
                from.UseBottledLiquefiedGasZgPhoto = from.UseBottledLiquefiedGasZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseBottledLiquefiedGasZgPhoto));
                from.UseStackedItemsZgPhoto = from.UseStackedItemsZgPhoto == null ? null : string.Join("|", JsonStr2List(from.UseStackedItemsZgPhoto));
                from.SlljdxZgPhoto = from.SlljdxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.SlljdxZgPhoto));
                from.YgbjwfwhyxPhoto = from.YgbjwfwhyxPhoto == null ? null : string.Join("|", JsonStr2List(from.YgbjwfwhyxPhoto));
                from.MhqwbcwhyxZgPhoto = from.MhqwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.MhqwbcwhyxZgPhoto));
                from.JyplmhxtwbcwhyxZgPhoto = from.JyplmhxtwbcwhyxZgPhoto == null ? null : string.Join("|", JsonStr2List(from.JyplmhxtwbcwhyxZgPhoto));
                //返回采集信息
                var ZmPcBase = _dbContext.ZmPcBaseCj.FirstOrDefault(u => u.Id == from.BaseId && u.IsDelete != 1);

                var r = new RespRet<object>(true, "Ok", new { ZmPcBaseCj = ZmPcBase, ZmPcYhCj = from });
                WriteSystemLog("housescj/search-yhlogdetaile", r, userNumber);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhlogdetaile", res, id);
                res.Msg = "系统异常";
                return res;
            }
        }

        private List<string> JsonStr2List(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }


        private object JsonRegion(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<object>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }


        //        /// <summary>
        //        /// 检查记录
        //        /// </summary>
        //        /// <param name="pageRequest"></param>
        //        /// <returns></returns>
        //        [HttpPost("housescj/searchcj-attryhzclog")]
        //        [Produces("application/json")]
        //        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogCjDto2>>), 200)]
        //        public RespRet<Page<ZmPcYhLogCjDto2>> SearchAttrList([FromBody] PageHouseRequestInputCj pageRequest)
        //        {
        //            if (pageRequest.PageIndex < 0)
        //            {
        //                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "PageIndex must >= 0");
        //            }
        //            var userNumber = this.CurrentUser();
        //            if (string.IsNullOrEmpty(userNumber))
        //            {
        //                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "user not logined");
        //            }
        //            try
        //            {
        //                var sbSql = new StringBuilder();
        //                var sbSqlWhere = new StringBuilder();
        //                var sbSqlWhere2 = new StringBuilder();
        //                var sqlParams = new List<NpgsqlParameter>();

        //                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
        //                {
        //                    sbSqlWhere.Append($@" and a.villagedm like @villagedm "); 
        //                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%"+pageRequest.Villagedm+"%"}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
        //                {
        //                    sbSqlWhere.Append($@" and a.Towndm like @Towndm ");
        //                    sqlParams.Add(new NpgsqlParameter("Towndm", $"{"%" + pageRequest.Towndm + "%"}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
        //                {
        //                    sbSqlWhere.Append($@" and a.Xzqdm like @Xzqdm ");
        //                    sqlParams.Add(new NpgsqlParameter("Xzqdm", $"{"%" + pageRequest.Xzqdm + "%"}"));
        //                }
        //                if (pageRequest.Pczt >= 1)
        //                {
        //                    sbSqlWhere.Append($@" and a.pczt =@pczt ");
        //                    sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.Pczt));
        //                }
        //                if (pageRequest.Yhly == 1)
        //                {
        //                    sbSqlWhere2.Append($@" and yhly='自查' ");
        //                }
        //                if (pageRequest.Yhly == 2)
        //                {
        //                    sbSqlWhere2.Append($@" and yhly='检查' ");
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Text))
        //                {
        //                    sbSqlWhere.Append($@" and (b.fwbh like @text or b.hzxm like @text or b.address like @text) ");
        //                    sqlParams.Add(new NpgsqlParameter("text", $"{"%" + pageRequest.Text + "%"}"));
        //                }
        //                if (pageRequest.CheckTime != null)
        //                {
        //                    if (pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbSqlWhere.Append($@" and date(create_on) between @start and @end  ");
        //                        sqlParams.Add(new NpgsqlParameter("start", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("end", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                sbSql.Append($@" select *from ( SELECT b.hzxm, b.address, a.id, a.fwbh, a.pczt, a.create_by, a.create_on, a.pcsj, a.jczt,'检查' yhly
        //FROM zm_pc_yhlog_cj a
        //JOIN zm_buildings b ON a.fwbh = b.fwbh where 1=1 {sbSqlWhere} 
        // union all
        //SELECT b.hzxm, b.address, a.id, a.fwbh, a.pczt, a.create_by, a.create_on, a.pcsj, a.jczt,'自查'yhly
        //FROM zm_pc_yhlog a
        //JOIN zm_buildings b ON a.fwbh = b.fwbh where 1=1 {sbSqlWhere}   )a  where 1=1 {sbSqlWhere2} ");
        //                var count = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
        //                sbSql.Append($@" order by create_on desc");
        //                sbSql.Append($@" limit @limit offset @offset");
        //                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
        //                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

        //                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<ZmPcYhLogCjDto2>(_)).ToList();


        //                var pageData = new Page<ZmPcYhLogCjDto2>(datas
        //                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);

        //                var r = new RespRet<Page<ZmPcYhLogCjDto2>>(true, "Ok", pageData);
        //                WriteSystemLog("housescj/search-attryhzclog", r, pageRequest);
        //                return r;
        //            }
        //            catch (Exception ex)
        //            {
        //                var res = new RespRet<Page<ZmPcYhLogCjDto2>>(false, ex.ToString());
        //                WriteSystemLogError("housescj/search-attryhzclog", res, pageRequest);
        //                res.Msg = "系统异常";
        //                return res;
        //            }
        //        }


        /// <summary>
        /// 自查记录、检查记录
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("housescj/searchcj-attryhzclog")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogAllDto2>>), 200)]
        public RespRet<Page<ZmPcYhLogAllDto2>> attrzclog([FromBody] PageHouseRequestInputCj pageRequest)
        {
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<Page<ZmPcYhLogAllDto2>>(false, "PageIndex must >= 0");
            }
            //var userNumber = this.CurrentUser();
            //if (string.IsNullOrEmpty(userNumber))
            //{
            //    return new RespRet<Page<ZmPcYhLogAllDto2>>(false, "user not logined");
            //}
            try
            {
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    sbSqlWhere.Append($@" and a.villagedm like @villagedm ");
                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + pageRequest.Villagedm + "%"}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {
                    sbSqlWhere.Append($@" and a.Towndm like @Towndm ");
                    sqlParams.Add(new NpgsqlParameter("Towndm", $"{"%" + pageRequest.Towndm + "%"}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSqlWhere.Append($@" and a.Xzqdm like @Xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("Xzqdm", $"{"%" + pageRequest.Xzqdm + "%"}"));
                }
                if (pageRequest.Pczt >= 1)
                {
                    sbSqlWhere.Append($@" and a.pczt =@pczt ");
                    sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.Pczt));
                }
                if (pageRequest.Yhly == 1)
                {
                    sbSqlWhere2.Append($@" and yhly='自查' ");
                }
                if (pageRequest.Yhly == 2)
                {
                    sbSqlWhere2.Append($@" and yhly='检查' ");
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Text))
                {
                    sbSqlWhere.Append($@" and (b.fwbh like @text or b.hzxm like @text or b.address like @text) ");
                    sqlParams.Add(new NpgsqlParameter("text", $"{"%" + pageRequest.Text + "%"}"));
                }
                if (pageRequest.CheckTime != null)
                {
                    if (pageRequest.CheckTime.Count == 2)
                    {
                        sbSqlWhere.Append($@" and date(create_on) between @start and @end  ");
                        sqlParams.Add(new NpgsqlParameter("start", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("end", pageRequest.CheckTime[1]));
                    }
                }
                sbSql.Append($@" select *from ( SELECT '' jcly,(select nickname create_name from zm_user_cj where number=a.create_by),b.hzxm, b.address, a.id, a.fwbh, a.pczt, a.create_by, a.create_on, a.pcsj, a.jczt,'检查' yhly
        FROM zm_pc_yhlog_cj a
        JOIN zm_buildings b ON a.fwbh = b.fwbh where 1=1 {sbSqlWhere} 
         union all
        SELECT  '' jcly,(select nickname create_name from zm_user_cj where number=a.create_by),b.hzxm, b.address, a.id, a.fwbh, a.pczt, a.create_by, a.create_on, a.pcsj, a.jczt,'自查'yhly
        FROM zm_pc_yhlog a
        JOIN zm_buildings b ON a.fwbh = b.fwbh where 1=1 {sbSqlWhere}   )a  where 1=1 {sbSqlWhere2} ");
                var count = _dbContext.ZmPcYhLogAllDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
                sbSql.Append($@" order by create_on desc");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

                var datas = _dbContext.ZmPcYhLogAllDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<ZmPcYhLogAllDto2>(_)).ToList();


                var pageData = new Page<ZmPcYhLogAllDto2>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);

                var r = new RespRet<Page<ZmPcYhLogAllDto2>>(true, "Ok", pageData);
                WriteSystemLog("housescj/searchcj-attralllog", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogAllDto2>>(false, ex.ToString());
                WriteSystemLogError("housescj/searchcj-attralllog", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }

        //        /// <summary>
        //        /// 检查记录核查记录返回房屋
        //        /// </summary>
        //        /// <param name="pageRequest"></param>
        //        /// <returns></returns>
        //        [HttpPost("housescj/searchcj-attryhzcfw")]
        //        [Produces("application/json")]
        //        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogCjDto2>>), 200)]
        //        public RespRet<Page<ZmPcYhLogCjDto2>> SearchAttrList([FromBody] PageHouseRequestInputCj pageRequest)
        //        {
        //            if (pageRequest.PageIndex < 0)
        //            {
        //                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "PageIndex must >= 0");
        //            }
        //            var userNumber = this.CurrentUser();
        //            if (string.IsNullOrEmpty(userNumber))
        //            {
        //                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "user not logined");
        //            }
        //            try
        //            {
        //                var sbSql = new StringBuilder();
        //                var sbCountSql = new StringBuilder();
        //                var sbSqlWhere = new StringBuilder();
        //                var sbSqlWhere2 = new StringBuilder();
        //                var sqlParams = new List<NpgsqlParameter>();
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
        //                {
        //                    sbSqlWhere.Append($@" and b.villagedm like @villagedm ");
        //                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + pageRequest.Villagedm + "%"}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
        //                {
        //                    sbSqlWhere.Append($@" and b.Towndm like @Towndm ");
        //                    sqlParams.Add(new NpgsqlParameter("Towndm", $"{"%" + pageRequest.Towndm + "%"}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
        //                {
        //                    sbSqlWhere.Append($@" and b.Xzqdm like @Xzqdm ");
        //                    sqlParams.Add(new NpgsqlParameter("Xzqdm", $"{"%" + pageRequest.Xzqdm + "%"}"));
        //                }

        //                if (!string.IsNullOrWhiteSpace(pageRequest.Text))
        //                {
        //                    sbSqlWhere.Append($@" and (b.fwbh like @text or b.hzxm like @text or b.address like @text) ");
        //                    sqlParams.Add(new NpgsqlParameter("text", $"{"%" + pageRequest.Text + "%"}"));
        //                }
        //                //默认全部
        //                sbSql.Append($@" SELECT  b.fwbh,b.address,b.hzxm
        //FROM  zm_buildings    b
        //full JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  ");
        //                sbCountSql.Append($@" SELECT count(1) count
        //FROM zm_buildings b
        //full JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  ");
        //                if (pageRequest.Pczt > 0)
        //                {

        //                    sbSqlWhere.Append($@" and ( zc.pczt=@pczt or cj.pczt=@pczt )  ");
        //                    sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.Pczt));
        //                }
        //                //if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                //{

        //                //}
        //                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 1)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //right JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS NOT NULL and zc.fwbh IS NOT NULL) ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //right JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS NOT NULL and zc.fwbh IS NOT NULL) ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 and (date(zc.create_on) between date(@starttime) and date(@endtime)   and date(cj.create_on) between date(@starttime) and date(@endtime))  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and (date(zc.create_on) between date(@starttime) and date(@endtime)   and date(cj.create_on) between date(@starttime) and date(@endtime))   ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Zczt == 2 && pageRequest.Jczt == 2)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //full JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS  NULL and zc.fwbh IS  NULL) ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //full JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS  NULL and zc.fwbh IS  NULL) ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and ((date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null )   and (date(cj.create_on) not between date(@starttime) and date(@endtime)) or cj.create_on is null )  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and ((date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null )   and (date(cj.create_on) not between date(@starttime) and date(@endtime)) or cj.create_on is null )  ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Zczt == 1 && pageRequest.Jczt == 2)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS  NULL and zc.fwbh IS NOT NULL) and b.fwbh is not null ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS  NULL and zc.fwbh IS NOT NULL) and b.fwbh is not null ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and date(zc.create_on)  between date(@starttime) and date(@endtime)   and ((date(cj.create_on) not between date(@starttime) and date(@endtime)) or cj.create_on is null )  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and date(zc.create_on)  between date(@starttime) and date(@endtime)   and ((date(cj.create_on) not between date(@starttime) and date(@endtime)) or cj.create_on is null )  ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }

        //                }
        //                else if (pageRequest.Zczt == 2 && pageRequest.Jczt == 1)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS NOT  NULL and zc.fwbh IS  NULL) and b.fwbh is not null  ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 AND (cj.fwbh IS not NULL and zc.fwbh IS  NULL) and b.fwbh is not null ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and ((date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null )   and date(cj.create_on)  between date(@starttime) and date(@endtime))  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        // left join zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1  and ((date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null )   and date(cj.create_on)  between date(@starttime) and date(@endtime))  ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Zczt == 1)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left  JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1 and zc.fwbh is not null   ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left  JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1 and zc.fwbh is not null  ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left  JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left  JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Zczt == 2)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1 and zc.fwbh is null ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1  and zc.fwbh is null ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1  and (date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null )  ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1   and ( date(zc.create_on) not between date(@starttime) and date(@endtime) or zc.create_on is null ) ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Jczt == 2)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 and cj.fwbh is null  ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 and cj.fwbh is null  ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1  and (date(cj.create_on) not between date(@starttime) and date(@endtime) or cj.create_on is null ) ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1   and (date(cj.create_on) not between date(@starttime) and date(@endtime) or cj.create_on is null )  ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }
        //                else if (pageRequest.Jczt == 1)
        //                {
        //                    sbCountSql = new StringBuilder();
        //                    sbSql = new StringBuilder();
        //                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 and cj.fwbh is not  null ");
        //                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //WHERE 1=1 and cj.fwbh is not null ");
        //                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
        //                    {
        //                        sbCountSql = new StringBuilder();
        //                        sbSql = new StringBuilder();
        //                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1  and date(cj.create_on)  between date(@starttime) and date(@endtime)   ");
        //                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
        //FROM zm_buildings b
        //left  JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
        //left JOIN zm_pc_yh_sqjz cj ON b.fwbh = cj.fwbh
        //WHERE 1=1   and date(cj.create_on)  between date(@starttime) and date(@endtime) ");
        //                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
        //                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
        //                    }
        //                }

        //                sbCountSql.Append(sbSqlWhere);
        //                sbSql.Append(sbSqlWhere);
        //                var count = _dbContext.CountClass.FromSqlRaw(sbCountSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
        //                sbSql.Append($@" order by fwbh ");
        //                sbSql.Append($@" limit @limit offset @offset");
        //                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
        //                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

        //                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
        //                var data = datas.Select(_ => _mapper.Map<ZmPcYhLogCjDto2>(_)).ToList();
        //                var pageData = new Page<ZmPcYhLogCjDto2>(data
        //                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count);
        //                var r = new RespRet<Page<ZmPcYhLogCjDto2>>(true, "Ok", pageData);
        //                WriteSystemLog("housescj/search-attryhzclog", r, pageRequest);
        //                return r;
        //            }
        //            catch (Exception ex)
        //            {
        //                var res = new RespRet<Page<ZmPcYhLogCjDto2>>(false, ex.ToString());
        //                WriteSystemLogError("housescj/search-attryhzclog", res, pageRequest);
        //                res.Msg = "系统异常";
        //                return res;
        //            }
        //        }






        /// <summary>
        /// 检查记录核查记录返回房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/searchcj-attryhzcfw")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogCjDto2>>), 200)]
        public RespRet<Page<ZmPcYhLogCjDto2>> SearchAttrList([FromBody] PageHouseRequestInputCj pageRequest)
        {
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<ZmPcYhLogCjDto2>>(false, "user not logined");
            }
            try
            {
                
                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    sbSqlWhere.Append($@" and b.villagedm like @villagedm ");
                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + pageRequest.Villagedm + "%"}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {
                    sbSqlWhere.Append($@" and b.Towndm like @Towndm ");
                    sqlParams.Add(new NpgsqlParameter("Towndm", $"{"%" + pageRequest.Towndm + "%"}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSqlWhere.Append($@" and b.Xzqdm like @Xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("Xzqdm", $"{"%" + pageRequest.Xzqdm + "%"}"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.Text))
                {
                    sbSqlWhere.Append($@" and (b.fwbh like @text or b.hzxm like @text or b.address like @text) ");
                    sqlParams.Add(new NpgsqlParameter("text", $"{"%" + pageRequest.Text + "%"}"));
                }
                //默认全部
                sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM  zm_buildings    b
full JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
full JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  ");
                sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
full JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
full JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
full JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  ");
                if (pageRequest.Pczt > 0)
                {

                    sbSqlWhere.Append($@" and ( zc.pczt=@pczt or cj.pczt=@pczt  or hc.pczt=@pczt )  ");
                    sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.Pczt));
                }
                #region 当自查为全部 ，所有组合
                //当自查为全部，检查为全部，核查为已核查
                if (pageRequest.Hczt == 1&&pageRequest.Zczt==0&&pageRequest.Jczt==0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and hc.fwbh is not null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and hc.fwbh is not null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and hc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and hc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为全部，检查为全部，核查为未核查
                if (pageRequest.Hczt == 2&&pageRequest.Jczt == 0&& pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and hc.fwbh is  null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and hc.fwbh is  null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and hc.fwbh is  null   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and hc.fwbh is  null   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为全部，检查为已检查，核查为全部
                if (pageRequest.Jczt == 1 && pageRequest.Hczt == 0 && pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is not  null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is not  null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is not  null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is not  null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为全部，检查为已检查，核查为已核查
                if (pageRequest.Jczt == 1&& pageRequest.Hczt == 1 && pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is not null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is not null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is not null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is not null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为全部，检查为已检查，核查为未核查
                if (pageRequest.Jczt == 1 && pageRequest.Hczt == 2&&pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is  null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is  null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is  null )   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is not  null and hc.fwbh is  null )   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为全部，检查为未检查，核查为全部
                if (pageRequest.Jczt == 2 && pageRequest.Hczt == 0 && pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is   null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is   null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is   null   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and cj.fwbh is   null   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为全部，检查未检查，核查为已核查
                if (pageRequest.Jczt == 2 && pageRequest.Hczt == 1&&pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is not null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is not  null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is not  null )   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is not null )   or ( date(zc.create_on) not  between date(@starttime) and date(@endtime))   ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为全部，检查未检查，核查为未核查
                if (pageRequest.Jczt == 2 && pageRequest.Hczt == 2&& pageRequest.Zczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is  null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is   null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is   null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (cj.fwbh is   null and hc.fwbh is  null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }
                #endregion


                #region 当自查为已自查 ，所有组合
                //当自查为已自查，检查为全部，核查为全部
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 0 && pageRequest.Hczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and zc.fwbh is not null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and zc.fwbh is not null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and zc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and zc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为已自查，检查为全部，核查为已核查
                if (pageRequest.Zczt == 1&&pageRequest.Hczt==1 && pageRequest.Jczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and zc.fwbh is not null and hc.fwbh is not null   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and zc.fwbh is not null and hc.fwbh is not null   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and zc.fwbh is not null and hc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and zc.fwbh is not null and hc.fwbh is not null   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为已自查，检查为全部，核查为未核查
                if (pageRequest.Zczt == 1 && pageRequest.Hczt == 2 && pageRequest.Jczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and hc.fwbh is  null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and hc.fwbh is  null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and hc.fwbh is  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and (zc.fwbh is not null and hc.fwbh is  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }



                //当自查为已自查，检查为已检查，核查为全部
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 1 && pageRequest.Hczt == 0 )
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is  null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is  null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and (zc.fwbh is not null and cj.fwbh is  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }



                //当自查为已自查，检查为已检查，核查为已核查
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 1&& pageRequest.Hczt == 1)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is not  null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is not  null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is not  null ) and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is not  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为已自查，检查为已检查，核查为未核查
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 1 && pageRequest.Hczt == 2)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is   null )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is   null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is   null ) and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1  and (zc.fwbh is not null and cj.fwbh is not  null and hc.fwbh is  null )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为已自查，检查为未检查，核查为全部
                if (pageRequest.Zczt == 1 && pageRequest.Jczt ==2 && pageRequest.Hczt == 0 )
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null  )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null  )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null  ) and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null  ) and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为已自查，检查为未检查，核查为已核查
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 2&&pageRequest.Hczt==1)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is not  null  )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is not  null  )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is not  null  )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is not  null  )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为已自查，检查为未检查，核查为未核查
                if (pageRequest.Zczt == 1 && pageRequest.Jczt == 2 && pageRequest.Hczt == 2)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is   null  )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is   null  )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is   null  )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is not null and cj.fwbh is   null and hc.fwbh is   null  )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }
                #endregion


                #region 当自查为未自查，所有组合
                //当自查为未自查，检查为全部，核查为为全部
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 0 && pageRequest.Hczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null  )  ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null  )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null  )  and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null  )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }
                

                //当自查为未自查，检查为全部，核查为已核查
                if (pageRequest.Zczt == 2&&pageRequest.Hczt==1 && pageRequest.Jczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is not null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is not null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is not null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is not null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为未自查，检查为全部，核查为未核查
                if (pageRequest.Zczt == 2 && pageRequest.Hczt == 2 && pageRequest.Jczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is  null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is  null )  ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is  null )   and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and hc.fwbh is  null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为未自查，检查为已检查，核查为全部
                if (pageRequest.Zczt == 2 && pageRequest.Jczt ==1 && pageRequest.Hczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为未自查，检查为已检查，核查为已核查
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 1&&pageRequest.Hczt==1)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is not  null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is not  null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is not  null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is not  null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为未自查，检查为已检查，核查为未核查
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 1 && pageRequest.Hczt == 2)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is   null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is   null )   ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is   null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is not  null and hc.fwbh is   null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }

                //当自查为未自查，检查为未核查，核查为全部
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 2 && pageRequest.Hczt == 0)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null )    ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为未自查，检查为未核查，核查为已核查
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 2&&pageRequest.Hczt==1)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is not null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is not null )    ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is not null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is not null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }


                //当自查为未自查，检查为未核查，核查为未核查
                if (pageRequest.Zczt == 2 && pageRequest.Jczt == 2 && pageRequest.Hczt == 2)
                {
                    sbCountSql = new StringBuilder();
                    sbSql = new StringBuilder();
                    sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is  null )   ");
                    sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is  null )    ");
                    if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count == 2)
                    {
                        sbCountSql = new StringBuilder();
                        sbSql = new StringBuilder();
                        sbSql.Append($@" SELECT distinct b.fwbh,b.address,b.hzxm
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is  null )    and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sbCountSql.Append($@" SELECT count(distinct b.fwbh) count
FROM zm_buildings b
left JOIN zm_pc_yh zc ON b.fwbh = zc.fwbh
left  JOIN zm_pc_yh_sqjz hc ON b.fwbh = hc.fwbh
left  JOIN zm_pc_yh_cj cj ON b.fwbh = cj.fwbh
WHERE 1=1 and (zc.fwbh is  null and cj.fwbh is   null and hc.fwbh is  null )     and date(zc.create_on)  between date(@starttime) and date(@endtime)  ");
                        sqlParams.Add(new NpgsqlParameter("starttime", pageRequest.CheckTime[0]));
                        sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.CheckTime[1]));
                    }
                }
                #endregion
                sbCountSql.Append(sbSqlWhere);
                sbSql.Append(sbSqlWhere);
                var count = _dbContext.CountClass.FromSqlRaw(sbCountSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var data = datas.Select(_ => _mapper.Map<ZmPcYhLogCjDto2>(_)).ToList();
                var pageData = new Page<ZmPcYhLogCjDto2>(data
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count);
                var r = new RespRet<Page<ZmPcYhLogCjDto2>>(true, "Ok", pageData);
                WriteSystemLog("housescj/searchcj-attryhzcfw", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto2>>(false, ex.ToString());
                WriteSystemLogError("housescj/searchcj-attryhzcfw", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }





        //        /// <summary>
        //        /// 房屋列表
        //        /// </summary>
        //        /// <param name="pageRequest"></param>
        //        /// <returns></returns>
        //        [HttpPost("housescj/search-fwlb")]
        //        [Produces("application/json")]
        //        [ProducesResponseType(typeof(RespRet<Page<FwlbDto>>), 200)]
        //        public RespRet<PagePc<FwlbDto>> Fwlb([FromBody] Fwlb pageRequest)
        //        {
        //            if (pageRequest == null)
        //            {
        //                return new RespRet<PagePc<FwlbDto>>(false, "请传递参数");
        //            }
        //            if (pageRequest.PageIndex < 0)
        //            {
        //                return new RespRet<PagePc<FwlbDto>>(false, "PageIndex must >= 0");
        //            }
        //            var userNumber = this.CurrentUser();
        //            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
        //            var a = JsonRegion(user.District);
        //            JObject obj = JObject.Parse(user.District);
        //            string RegionCode = (string)obj["RegionCode"];
        //            if (string.IsNullOrEmpty(userNumber))
        //            {
        //                return new RespRet<PagePc<FwlbDto>>(false, "user not logined");
        //            }
        //            try
        //            {
        //                var sbSql = new StringBuilder();
        //                var sbSqlWhere = new StringBuilder();
        //                var sqlParams = new List<NpgsqlParameter>();
        //                sbSql.Append($@" SELECT
        // b.fwbh as id,
        //    b.address, 
        //    b.hzxm, 
        //    b.fwbh, 
        //    a.pczt,
        //    st_asgeojson(geom) AS geometry,
        // CASE
        //        WHEN geom IS NULL THEN NULL
        //        ELSE json_build_object(
        //            'type', 'Feature',
        //            'geometry', json_build_object(
        //                'type', 'MultiPolygon',
        //                'coordinates', ST_AsGeoJSON(geom)::json->'coordinates'
        //            ),
        //            'properties', json_build_object(
        //                'fwbh', b.fwbh,
        //                'address',b.address,
        //                'hzxm', b.hzxm
        //            )
        //        )
        //    END AS feature,
        //		d.pczt zczt,
        //	case when	DATE_PART('day', now() -d.create_on)::text is null then '' else DATE_PART('day', now() -d.create_on)::text end AS day_difference		
        //FROM 
        //    (select t1.*,t2.fwzt from zm_buildings t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh  where fwzt is   null) b  
        //LEFT JOIN 
        //    (select *from zm_pc_yh_cj where create_on >= DATE_TRUNC('month', CURRENT_DATE) 
        //             AND  create_on < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' ) a ON b.fwbh = a.fwbh
        //LEFT JOIN 
        //    ( select *from zm_pc_yh where create_on >= DATE_TRUNC('month', CURRENT_DATE) 
        //             AND  create_on < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' ) d ON d.fwbh = b.fwbh  where 1=1  ");
        //                var CountSql = new StringBuilder();
        //                CountSql.Append($@"  SELECT
        // count(case when d.pczt is null then 1 end) wzc,
        // count(case when d.pczt is not null then 1 end) yzc,
        // count(case when a.fwbh is null then 1 end) wjc,
        // count(case when a.fwbh is not null then 1 end) yjc
        //FROM 
        //    (select t1.*,t2.fwzt from zm_buildings t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh  where fwzt is   null) b  
        //LEFT JOIN 
        //  (select *from zm_pc_yh_cj where create_on >= DATE_TRUNC('month', CURRENT_DATE) 
        //             AND  create_on < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' ) a ON b.fwbh = a.fwbh
        //LEFT JOIN 
        //  ( select *from zm_pc_yh where create_on >= DATE_TRUNC('month', CURRENT_DATE) 
        //             AND  create_on < DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' ) d ON d.fwbh = b.fwbh  where 1=1  ");
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
        //                {
        //                    CountSql.Append($@" and b.villagedm=@villagedm ");
        //                    sbSql.Append($@" and b.villagedm=@villagedm ");
        //                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
        //                {
        //                    CountSql.Append($@" and b.towndm=@towndm ");
        //                    sbSql.Append($@" and b.towndm=@towndm ");
        //                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
        //                }
        //                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
        //                {
        //                    CountSql.Append($@" and b.xzqdm=@xzqdm ");
        //                    sbSql.Append($@" and b.xzqdm=@xzqdm ");
        //                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
        //                }
        //                if (pageRequest.pczt != null)
        //                {
        //                    if (pageRequest.pczt == 0)
        //                    {
        //                        CountSql.Append($@" and a.pczt is null ");
        //                        sbSql.Append($@" and a.pczt is null ");
        //                    }
        //                    else
        //                    {
        //                        CountSql.Append($@" and a.pczt=@pczt ");
        //                        sbSql.Append($@" and a.pczt=@pczt ");
        //                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.pczt));
        //                    }
        //                }
        //                //if (pageRequest.zczt != null)
        //                //{
        //                //    if (pageRequest.zczt == 0)
        //                //    {
        //                //        CountSql.Append($@" and d.pczt is null ");
        //                //        sbSql.Append($@" and d.pczt is null ");
        //                //    }
        //                //    else
        //                //    {
        //                //        CountSql.Append($@" and d.pczt=@zczt ");
        //                //        sbSql.Append($@" and d.pczt=@zczt ");
        //                //        sqlParams.Add(new NpgsqlParameter("zczt", pageRequest.zczt));
        //                //    }
        //                //}
        //                if (pageRequest.ispc != null)
        //                {
        //                    if (pageRequest.ispc == 0)
        //                    {
        //                        CountSql.Append($@" and a.pczt is null ");
        //                        sbSql.Append($@" and a.pczt is null ");
        //                    }
        //                    else
        //                    {
        //                        CountSql.Append($@" and a.pczt is not null ");
        //                        sbSql.Append($@" and a.pczt is not null ");
        //                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.ispc));
        //                    }
        //                }
        //                //if (pageRequest.iszc != null)
        //                //{
        //                //    if (pageRequest.iszc == 0)
        //                //    {
        //                //        CountSql.Append($@" and d.zczt is null ");
        //                //        sbSql.Append($@" and d.zczt is null ");
        //                //    }
        //                //    else
        //                //    {
        //                //        CountSql.Append($@" and d.pczt is not null ");
        //                //        sbSql.Append($@" and d.pczt  is not null ");
        //                //        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.iszc));
        //                //    }
        //                //}
        //                if (!string.IsNullOrWhiteSpace(pageRequest.text))
        //                {
        //                    CountSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
        //                    sbSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
        //                    sqlParams.Add(new NpgsqlParameter("text", "%" + pageRequest.text + "%"));
        //                }
        //                var count = _dbContext.FwlbDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
        //                var countpc = _dbContext.FwlbCount.FromSqlRaw(CountSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
        //                sbSql.Append($@" order by @order @desc");
        //                if (pageRequest.OrderBy == 0)
        //                {
        //                    sbSql.Replace("@order", " case when DATE_PART('day', now() -d.create_on)::text is null then 0 else DATE_PART('day', now() -d.create_on) end   ");
        //                }
        //                else if (pageRequest.OrderBy == 1)
        //                {
        //                    sbSql.Replace("@order", " b.address ");
        //                }
        //                else if (pageRequest.OrderBy == 2)
        //                {
        //                    sbSql.Replace("@order", " d.pczt ");
        //                }
        //                else if (pageRequest.OrderBy == 3)
        //                {
        //                    sbSql.Replace("@order", " a.pczt ");
        //                }
        //                if (pageRequest.OrderDesc == false)
        //                {
        //                    sbSql.Replace("@desc", " asc ");
        //                }
        //                else
        //                {
        //                    sbSql.Replace("@desc", " desc ");
        //                }
        //                sbSql.Append($@" limit @limit offset @offset");
        //                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
        //                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
        //                var datas = _dbContext.FwlbDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<FwlbDto>(_)).ToList();
        //                var pageData = new PagePc<FwlbDto>(datas
        //                    , pageRequest.PageIndex + 1, pageRequest.Limit, count, countpc.Yzc, countpc.Wzc, countpc.Yjc, countpc.Wjc);
        //                var r = new RespRet<PagePc<FwlbDto>>(true, "Ok", pageData);
        //                WriteSystemLog("housescj/search-fwlb", r, pageRequest);
        //                return r;
        //            }
        //            catch (Exception ex)
        //            {
        //                var res = new RespRet<PagePc<FwlbDto>>(false, ex.ToString());
        //                WriteSystemLogError("housescj/search-fwlb", res, pageRequest);
        //                res.Msg = "系统异常";
        //                return res;
        //            }
        //        }




        /// <summary>
        /// 房屋列表
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-fwlb")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<Fwlb_Fwzt>>), 200)]
        public RespRet<PagePc<Fwlb_Fwzt>> Fwlb([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<PagePc<Fwlb_Fwzt>>(false, "请传递参数");
            }
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<PagePc<Fwlb_Fwzt>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<PagePc<Fwlb_Fwzt>>(false, "user not logined");
            }
            try
            {

                var sbSql = new StringBuilder();
                var sbSqlCount = new StringBuilder();
                var yccbczsql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                string time = DateTime.Today
    .AddHours(23)
    .AddMinutes(59)
    .AddSeconds(59)
    .ToString("yyyy-MM-dd HH:mm:ss");
                sqlParams.Add(new NpgsqlParameter("time", time));

                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                string fwzt_where = "";

                if (pageRequest.fwzt != 0)
                {
                    fwzt_where = $" select * from zm_buildings t1 where fwbh  in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt=@fwzt  ) ";

                }
                else
                {
                    fwzt_where = $" select * from zm_buildings t1 where fwbh  not in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt is not null  )  ";
                }
                sqlParams.Add(new NpgsqlParameter("fwzt", pageRequest.fwzt));

                sbSql.Append($@" SELECT
case when @fwzt=0 then null else @fwzt  end fwzt,
 b.fwbh as id,
 max(date(a.create_on)::text) jcsj,
 max(date(a.create_on)::text) hcsj,
 max(date(d.create_on)::text) zcsj,
   max(b.address) address, 
   max(b.hzxm) hzxm, 
    b.fwbh, 
		  ST_X(ST_Centroid(ST_Collect(geom))) AS villagelongitude,
    ST_Y(ST_Centroid(ST_Collect(geom))) AS villagelatitude,
   max(a.pczt) pczt,
  max(st_asgeojson(geom)) AS geometry,
 CASE
        WHEN max(geom) IS NULL THEN NULL
        ELSE json_build_object(
            'type', 'Feature',
            'geometry', json_build_object(
                'type', 'MultiPolygon',
                'coordinates', ST_AsGeoJSON(max(geom))::json->'coordinates'
            ),
            'properties', json_build_object(
                'fwbh', max(b.fwbh),
                'address',max(b.address),
                'hzxm',max(b.hzxm)
            )
        )
    END AS feature,
	max(d.pczt)zczt,
	max(d.pczt)jczt,
	max(d.pczt)hczt,
 max(case when	DATE_PART('day', now() -d.create_on)::text is null then '' else DATE_PART('day', now() -d.create_on)::text end )AS day_difference		
FROM 
     ({fwzt_where})  b   
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  ");
                sbSqlCount.Append($@" SELECT
 count(distinct b.fwbh) count	,
  1 yjc,2 wjc,1 yzc,2 wzc
FROM 
          ({fwzt_where})  b     
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  ");
                yccbczsql.Append($@" SELECT
 count(1) count	,
1 yjc,2 wjc,1 yzc,2 wzc
FROM 
           ({fwzt_where})  b   
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  ");

                var CountSql = new StringBuilder();
                CountSql.Append($@"  SELECT
 count(case when d.pczt is null then 1 end) wzc,
 count(case when d.pczt is not null then 1 end) yzc,
 count(case when a.fwbh is null then 1 end) wjc,
 count(case when a.fwbh is not null then 1 end) yjc,
1 count 
FROM 
     ({fwzt_where} )  b     
LEFT JOIN 
   zm_pc_yh_cj a  ON b.fwbh = a.fwbh 
LEFT JOIN 
  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    CountSql.Append($@" and b.villagedm=@villagedm ");
                    sbSql.Append($@" and b.villagedm=@villagedm ");
                    sbSqlCount.Append($@" and b.villagedm=@villagedm ");
                    yccbczsql.Append($@" and b.villagedm=@villagedm ");
                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                //if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                //{
                //    CountSql.Append($@" and b.towndm=@towndm ");
                //    sbSql.Append($@" and b.towndm=@towndm ");
                //    sbSqlCount.Append($@" and b.towndm=@towndm ");
                //    yccbczsql.Append($@" and b.towndm=@towndm ");
                //    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                //}
                //if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                //{
                //    CountSql.Append($@" and b.xzqdm=@xzqdm ");
                //    sbSql.Append($@" and b.xzqdm=@xzqdm ");
                //    sbSqlCount.Append($@" and b.xzqdm=@xzqdm ");
                //    yccbczsql.Append($@" and b.xzqdm=@xzqdm ");
                //    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                //}
                if (pageRequest.pczt != null)
                {
                    if (pageRequest.pczt == 0)
                    {
                        CountSql.Append($@" and a.pczt is null ");
                        sbSql.Append($@" and a.pczt is null ");
                        yccbczsql.Append($@" and a.pczt is null ");
                        sbSqlCount.Append($@" and a.pczt is null ");
                    }
                    else
                    {
                        CountSql.Append($@" and a.pczt=@pczt ");
                        sbSql.Append($@" and a.pczt=@pczt ");
                        sbSqlCount.Append($@" and a.pczt=@pczt ");
                        yccbczsql.Append($@" and a.pczt=@pczt ");
                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.pczt));
                    }
                }

                if (pageRequest.ispc != null)
                {
                    if (pageRequest.ispc == 0)
                    {
                        CountSql.Append($@" and a.pczt is null ");
                        sbSql.Append($@" and a.pczt is null ");
                        yccbczsql.Append($@" and a.pczt is null ");
                        sbSqlCount.Append($@" and a.pczt is null ");
                    }
                    else
                    {
                        CountSql.Append($@" and a.pczt is not null ");
                        sbSql.Append($@" and a.pczt is not null ");
                        sbSqlCount.Append($@" and a.pczt is not null ");
                        yccbczsql.Append($@" and a.pczt is not null ");
                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.ispc));
                    }
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.text))
                {
                    CountSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sbSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sbSqlCount.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    yccbczsql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sqlParams.Add(new NpgsqlParameter("text", "%" + pageRequest.text + "%"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    CountSql.Append($@" and ( b.fwbh like @fwbh ) ");
                    sbSql.Append($@" and (  b.fwbh like @fwbh ) ");
                    sbSqlCount.Append($@" and (  b.fwbh like @fwbh ) ");
                    yccbczsql.Append($@" and (  b.fwbh like @fwbh ) ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }

                //if (pageRequest.fwzt != 0)
                //{
                //    CountSql.Append($@" and ( b.fwzt =@fwzt  ) ");
                //    sbSql.Append($@" and (  b.fwzt =@fwzt )  ");
                //    sbSqlCount.Append($@" and (  b.fwzt =@fwzt )  ");
                //    yccbczsql.Append($@" and (  b.fwzt =@fwzt  )  ");
                //    sqlParams.Add(new NpgsqlParameter("fwzt", pageRequest.fwzt));
                //}
                //else
                //{
                //    sbSqlCount.Append($@" and ( b.fwzt is null  )   ");
                //    CountSql.Append($@" and ( b.fwzt is null  ) ");
                //    sbSql.Append($@" and ( b.fwzt is null  )  ");
                //    yccbczsql.Append($@" and ( b.fwzt is null  )  ");
                //}
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    CountSql.Append($@" and ( b.hzxm like @hzxm ) ");
                    sbSql.Append($@" and (  b.hzxm like @hzxm ) ");
                    sbSqlCount.Append($@" and (  b.hzxm like @hzxm ) ");
                    yccbczsql.Append($@" and (  b.hzxm like @hzxm ) ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    CountSql.Append($@" and ( b.address like @address ) ");
                    sbSql.Append($@" and (  b.address like @address ) ");
                    sbSqlCount.Append($@" and (  b.address like @address ) ");
                    yccbczsql.Append($@" and (  b.address like @address ) ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                sbSql.Append($@" group by b.fwbh ");
                var count = _dbContext.FwlbCount.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var yccbcz = _dbContext.FwlbCount.FromSqlRaw(yccbczsql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                //var countpc = _dbContext.FwlbCount.FromSqlRaw(CountSql.ToString(), sqlParams.ToArray()).FirstOrDefault();

                //根据不同角色  进行不同排序
                if (user.RoleType == 2 || user.RoleType == 1 || user.RoleType == 3)
                {
                    sbSql.Append($@" order by max(d.create_on) desc NULLS LAST,max(a.create_on) desc NULLS LAST,b.fwbh ");

                }
                else
                {
                    sbSql.Append($@" 	order by max(case when DATE_PART('day', now() -a.create_on)::text is null then null else DATE_PART('day', now() -a.create_on) end )desc,max(case when DATE_PART('day', now() -d.create_on)::text is null then null else DATE_PART('day', now() -d.create_on) end )desc,b.fwbh
 ");
                }
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                var datas = _dbContext.Fwlb_Fwzt.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<Fwlb_Fwzt>(_)).ToList();
                var pageData = new PagePc<Fwlb_Fwzt>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count, count.Count, count.Yzc, count.Wzc, count.Yjc, count.Wjc);
                var r = new RespRet<PagePc<Fwlb_Fwzt>>(true, "Ok", pageData);
                WriteSystemLog("housescj/search-fwlb", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<PagePc<Fwlb_Fwzt>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-fwlb", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 近七天为自查房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/TheWeekNoCheckHouse")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<FwlbDto>>), 200)]
        public RespRet<int> TheWeekNoCheckHouse([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<int>(false, "请传递参数");
            }
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<int>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<int>(false, "user not logined");
            }
            try
            {
                var sbSqlCount = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                sbSqlCount.Append($@"  SELECT
count(distinct b.fwbh) count	
FROM 
     (select t1.*,t2.fwzt from zm_buildings t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh where fwzt is   null)  b     
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  and date(now())- date(d.create_on)>7 and b.fwbh not in (select fwbh From zm_pc_base_sqjz where zm_pc_base_sqjz.yhly='4' and fwzt is not null) and b.fwbh in (select fwbh From zm_pc_base)
  ");

                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {

                    sbSqlCount.Append($@" and b.villagedm=@villagedm ");

                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {
                    sbSqlCount.Append($@" and b.towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSqlCount.Append($@" and b.xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                var datas = _dbContext.CountClass.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var r = new RespRet<int>(true, "Ok", datas.Count);
                WriteSystemLog("housescj/TheWeekNoCheckHouse", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("housescj/TheWeekNoCheckHouse", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 近七天为自查房屋详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/NoCheckHouseDetaile")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<FwlbDto>>), 200)]
        public RespRet<PagePc<FwlbDto>> NoCheckHouseDetaile([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<PagePc<FwlbDto>>(false, "请传递参数");
            }
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<PagePc<FwlbDto>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<PagePc<FwlbDto>>(false, "user not logined");
            }
            try
            {
                string time = DateTime.Today
        .AddHours(23)
        .AddMinutes(59)
        .AddSeconds(59)
        .ToString("yyyy-MM-dd HH:mm:ss");


                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;
                var sbSql = new StringBuilder();
                var sbSqlCount = new StringBuilder();
                var yccbczsql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();


                sqlParams.Add(new NpgsqlParameter("time", time));
                sbSql.Append($@" SELECT
 b.fwbh as id,
max(date(a.create_on)::text) hcsj,
 max(date(a.create_on)::text) jcsj,
 max(date(d.create_on)::text) zcsj,
   max(b.address) address, 
   max(b.hzxm) hzxm, 
    b.fwbh, 
		  ST_X(ST_Centroid(ST_Collect(geom))) AS villagelongitude,
    ST_Y(ST_Centroid(ST_Collect(geom))) AS villagelatitude,
   max(a.pczt) pczt,
  max(st_asgeojson(geom)) AS geometry,
 CASE
        WHEN max(geom) IS NULL THEN NULL
        ELSE json_build_object(
            'type', 'Feature',
            'geometry', json_build_object(
                'type', 'MultiPolygon',
                'coordinates', ST_AsGeoJSON(max(geom))::json->'coordinates'
            ),
            'properties', json_build_object(
                'fwbh', max(b.fwbh),
                'address',max(b.address),
                'hzxm',max(b.hzxm)
            )
        )
    END AS feature,
	max(d.pczt)zczt,
max(d.pczt)jczt,
 max(case when	DATE_PART('day', now() -d.create_on)::text is null then '' else DATE_PART('day', now() -d.create_on)::text end )AS day_difference		
FROM 
( select * from zm_buildings t1 where fwbh  not in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt is not null  )  )  b    
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  and b.fwbh in (select fwbh From zm_pc_base) ");

                sbSqlCount.Append($@" SELECT
 count(distinct b.fwbh) count	,
  1 yjc,2 wjc,1 yzc,2 wzc
FROM 
( select * from zm_buildings t1 where fwbh  not in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt is not null  ) )  b   
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1 and b.fwbh in (select fwbh From zm_pc_base)  ");
                yccbczsql.Append($@" SELECT
 count(1) count	,
1 yjc,2 wjc,1 yzc,2 wzc
FROM 
      (select * from zm_buildings t1 where fwbh  not in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt is not null  )  )  b   
LEFT JOIN 
     zm_pc_yh_cj  a ON b.fwbh = a.fwbh
LEFT JOIN  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1  and b.fwbh in (select fwbh From zm_pc_base) ");

                var CountSql = new StringBuilder();
                CountSql.Append($@"  SELECT
 count(case when d.pczt is null then 1 end) wzc,
 count(case when d.pczt is not null then 1 end) yzc,
 count(case when a.fwbh is null then 1 end) wjc,
 count(case when a.fwbh is not null then 1 end) yjc,
1 count 
FROM 
( select * from zm_buildings t1 where fwbh  not in  (select fwbh From  {table_name} where final_create_on=@time  and final_fwzt is not null  )  )  b   
LEFT JOIN 
   zm_pc_yh_cj a  ON b.fwbh = a.fwbh 
LEFT JOIN 
  zm_pc_yh  d ON d.fwbh = b.fwbh  where 1=1 and b.fwbh in (select fwbh From zm_pc_base)  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    CountSql.Append($@" and b.villagedm=@villagedm ");
                    sbSql.Append($@" and b.villagedm=@villagedm ");
                    sbSqlCount.Append($@" and b.villagedm=@villagedm ");
                    yccbczsql.Append($@" and b.villagedm=@villagedm ");
                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {
                    CountSql.Append($@" and b.towndm=@towndm ");
                    sbSql.Append($@" and b.towndm=@towndm ");
                    sbSqlCount.Append($@" and b.towndm=@towndm ");
                    yccbczsql.Append($@" and b.towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    CountSql.Append($@" and b.xzqdm=@xzqdm ");
                    sbSql.Append($@" and b.xzqdm=@xzqdm ");
                    sbSqlCount.Append($@" and b.xzqdm=@xzqdm ");
                    yccbczsql.Append($@" and b.xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                if (pageRequest.pczt != null)
                {
                    if (pageRequest.pczt == 0)
                    {
                        CountSql.Append($@" and a.pczt is null ");
                        sbSql.Append($@" and a.pczt is null ");
                        yccbczsql.Append($@" and a.pczt is null ");
                        sbSqlCount.Append($@" and a.pczt is null ");
                    }
                    else
                    {
                        CountSql.Append($@" and a.pczt=@pczt ");
                        sbSql.Append($@" and a.pczt=@pczt ");
                        sbSqlCount.Append($@" and a.pczt=@pczt ");
                        yccbczsql.Append($@" and a.pczt=@pczt ");
                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.pczt));
                    }
                }

                if (pageRequest.ispc != null)
                {
                    if (pageRequest.ispc == 0)
                    {
                        CountSql.Append($@" and a.pczt is null ");
                        sbSql.Append($@" and a.pczt is null ");
                        yccbczsql.Append($@" and a.pczt is null ");
                        sbSqlCount.Append($@" and a.pczt is null ");
                    }
                    else
                    {
                        CountSql.Append($@" and a.pczt is not null ");
                        sbSql.Append($@" and a.pczt is not null ");
                        sbSqlCount.Append($@" and a.pczt is not null ");
                        yccbczsql.Append($@" and a.pczt is not null ");
                        sqlParams.Add(new NpgsqlParameter("pczt", pageRequest.ispc));
                    }
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.text))
                {
                    CountSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sbSql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sbSqlCount.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    yccbczsql.Append($@" and ( b.hzxm like @text or b.address like @text or b.fwbh like @text ) ");
                    sqlParams.Add(new NpgsqlParameter("text", "%" + pageRequest.text + "%"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    CountSql.Append($@" and ( b.fwbh like @fwbh ) ");
                    sbSql.Append($@" and (  b.fwbh like @fwbh ) ");
                    sbSqlCount.Append($@" and (  b.fwbh like @fwbh ) ");
                    yccbczsql.Append($@" and (  b.fwbh like @fwbh ) ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    CountSql.Append($@" and ( b.hzxm like @hzxm ) ");
                    sbSql.Append($@" and (  b.hzxm like @hzxm ) ");
                    sbSqlCount.Append($@" and (  b.hzxm like @hzxm ) ");
                    yccbczsql.Append($@" and (  b.hzxm like @hzxm ) ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }

                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    CountSql.Append($@" and ( b.address like @address ) ");
                    sbSql.Append($@" and (  b.address like @address ) ");
                    sbSqlCount.Append($@" and (  b.address like @address ) ");
                    yccbczsql.Append($@" and (  b.address like @address ) ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                CountSql.Append($@"  and date(now())- date(d.create_on)>7 ");
                sbSql.Append($@"  and date(now())- date(d.create_on)>7");
                sbSqlCount.Append($@" and date(now())- date(d.create_on)>7 ");
                yccbczsql.Append($@"  and date(now())- date(d.create_on)>7 ");


                sbSql.Append($@" group by b.fwbh ");

                var count = _dbContext.FwlbCount.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var yccbcz = _dbContext.FwlbCount.FromSqlRaw(yccbczsql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var countpc = _dbContext.FwlbCount.FromSqlRaw(CountSql.ToString(), sqlParams.ToArray()).FirstOrDefault();

                //根据不同角色  进行不同排序
                if (user.RoleType == 2 || user.RoleType == 1 || user.RoleType == 3)
                {
                    sbSql.Append($@" order by max(d.create_on) desc NULLS LAST,max(a.create_on) desc NULLS LAST,b.fwbh ");

                }
                else
                {
                    sbSql.Append($@" 	order by max(date(now())- date(a.create_on))desc,max(date(now())- date(d.create_on))desc,b.fwbh
 ");
                }
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                var datas = _dbContext.FwlbDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<FwlbDto>(_)).ToList();
                var pageData = new PagePc<FwlbDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count, yccbcz.Count, countpc.Yzc, countpc.Wzc, countpc.Yjc, countpc.Wjc);
                var r = new RespRet<PagePc<FwlbDto>>(true, "Ok", pageData);
                WriteSystemLog("housescj/search-fwlb", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<PagePc<FwlbDto>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-fwlb", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 上个月未自查房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/TheMonthNoCheckHouse")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<MyHouseDto>>), 200)]
        public RespRet<int> TheMonthNoCheckHouse([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<int>(false, "请传递参数");
            }
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<int>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<int>(false, "user not logined");
            }
            try
            {
                var sbSqlCount = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                string time = DateTime.Today
                          .AddHours(23)
                          .AddMinutes(59)
                          .AddSeconds(59)
                          .ToString("yyyy-MM-dd HH:mm:ss");

                DateTime today = DateTime.Today;

                var prevMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);
                var year = prevMonth.Year;
                var month = prevMonth.ToString("MM");
                string tablename = "zm_pc_baselog_" + year + "_" + month;


                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;
                sbSqlCount.Append($@"  select count(1) FROM zm_buildings b where fwbh not in (select fwbh from  {table_name} where final_create_on='"+ time + "' and final_fwzt is not null )  and  fwbh not in (select distinct(fwbh) from  "+tablename+" )");

                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {

                    sbSqlCount.Append($@" and b.villagedm=@villagedm ");

                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {
                    sbSqlCount.Append($@" and b.towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSqlCount.Append($@" and b.xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                var datas = _dbContext.CountClass.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var r = new RespRet<int>(true, "Ok", datas.Count);
                WriteSystemLog("housescj/TheMonthNoCheckHouse", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("housescj/TheMonthNoCheckHouse", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 上个月未自查房屋列表 
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/TheMonthNoCheckFwlb")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<MyHouseDto>>), 200)]
        public RespRet<Page<MyHouseDto>> MonthNoCheckFwlb([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<Page<MyHouseDto>>(false, "参数不正确");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<MyHouseDto>>(false, "user not logined");
            }
            try
            {
                var sbSql = new StringBuilder();
                var sbSqlCount = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                string time = DateTime.Today
                          .AddHours(23)
                          .AddMinutes(59)
                          .AddSeconds(59)
                          .ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                DateTime today = DateTime.Today;

                var prevMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);
                var year = prevMonth.Year;
                var month = prevMonth.ToString("MM");
                string tablename = "zm_pc_baselog_" + year + "_" + month;


                sbSql.Append($@"  select b.fwbh,concat(b.xzq, '-', b.town, '-', b.village) AS szc,b.address,b.hzxm,null fwzt   FROM zm_buildings b where fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null )  and  fwbh not in (select distinct(fwbh) from  "+ tablename + ")");

                sbSqlCount.Append($@"  select count(1) FROM zm_buildings b where fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null )  and  fwbh not in (select distinct(fwbh) from  "+tablename+")");


                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    sbSql.Append($@" and b.villagedm=@villagedm ");
                    sbSqlCount.Append($@" and b.villagedm=@villagedm ");

                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {

                    sbSql.Append($@" and b.towndm=@towndm ");

                    sbSqlCount.Append($@" and b.towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSql.Append($@" and b.xzqdm=@xzqdm ");

                    sbSqlCount.Append($@" and b.xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                var count = _dbContext.CountClass.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();

                sbSql.Append($@" order by b.fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

                var datas = _dbContext.MyHouseDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();

                var pageData = new Page<MyHouseDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count);

                var r = new RespRet<Page<MyHouseDto>>(true, "Ok", pageData);
                WriteSystemLog("housescj/TheMonthNoCheckFwlb", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<MyHouseDto>>(false, ex.ToString());
                WriteSystemLogError("housescj/TheMonthNoCheckFwlb", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 房屋预警 房屋基本信息与核验结果矛盾 
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/TheBaseinfoUnmatch")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<MyHouseDto>>), 200)]
        public RespRet<Page<MyHouseDtoYJ>> TheBaseinfoUnmatch([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<Page<MyHouseDtoYJ>>(false, "参数不正确");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<MyHouseDtoYJ>>(false, "user not logined");
            }
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
            var a = JsonRegion(user.District);

            try
            {
                var sbSql = new StringBuilder();
                var sbSqlCount = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                string time = DateTime.Today
                          .AddHours(23)
                          .AddMinutes(59)
                          .AddSeconds(59)
                          .ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                DateTime today = DateTime.Today;

                var prevMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);
                var year = prevMonth.Year;
                var month = prevMonth.ToString("MM");
       

                sbSql.Append($@" select * from (SELECT 
								xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								create_on AS qj_create_on,
								jsrysstd AS qj_jsrysstd,
								'区级房屋基本信息与核验不一致' AS qj_remark,
								cs,
								jzscmj,
								jzscxyaqcksl,
								jzxyssltsl,yhly
						FROM (
								SELECT 
										m1.*,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								JOIN 
										zm_pc_yhlog_sqjz m2 ON m1.id = m2.base_id
								WHERE 
										m1.yhly = '3'
						) t
						WHERE row_num = 1  
						AND (
								 ((cs = 1 AND jzscmj > 200 AND jzscxyaqcksl <= 1) AND jsrysstd = '是') 
								OR 
								((cs >= 2 AND jzscxyaqcksl < 2 AND jzxyssltsl < 2) AND jsrysstd = '是')
						)
						
						union all
							SELECT 
						 xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								create_on AS sj_create_on,
								jsrysstd AS sj_jsrysstd,
								'市级房屋基本信息与核验不一致'  AS sj_remark,
								cs,
								jzscmj,
								jzscxyaqcksl,
								jzxyssltsl,yhly
						FROM (
								SELECT 
										m1.*,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								JOIN 
										zm_pc_yhlog_sqjz m2 ON m1.id = m2.base_id
								WHERE 
										m1.yhly = '2'						) t
						WHERE row_num = 1  
						AND (
								((cs = 1 AND jzscmj > 200 AND jzscxyaqcksl <= 1) AND jsrysstd = '是') 
								OR 
								((cs >= 2 AND jzscxyaqcksl < 2 AND jzxyssltsl < 2) AND jsrysstd = '是')
						))  t1  where fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null ) ");

                sbSqlCount.Append($@"  select count(1) FROM ( select * from (SELECT 
								xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								create_on AS qj_create_on,
								jsrysstd AS qj_jsrysstd,
								'区级房屋基本信息与核验不一致' AS qj_remark,
								cs,
								jzscmj,
								jzscxyaqcksl,
								jzxyssltsl,yhly
						FROM (
								SELECT 
										m1.*,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								JOIN 
										zm_pc_yhlog_sqjz m2 ON m1.id = m2.base_id
								WHERE 
										m1.yhly = '3'
						) t
						WHERE row_num = 1  
						AND (
								 ((cs = 1 AND jzscmj > 200 AND jzscxyaqcksl <= 1) AND jsrysstd = '是') 
								OR 
								((cs >= 2 AND jzscxyaqcksl < 2 AND jzxyssltsl < 2) AND jsrysstd = '是')
						)
						
						union all
							SELECT 
						 xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								create_on AS sj_create_on,
								jsrysstd AS sj_jsrysstd,
								'市级房屋基本信息与核验不一致'  AS sj_remark,
								cs,
								jzscmj,
								jzscxyaqcksl,
								jzxyssltsl,yhly
						FROM (
								SELECT 
										m1.*,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								JOIN 
										zm_pc_yhlog_sqjz m2 ON m1.id = m2.base_id
								WHERE 
										m1.yhly = '2'						) t
						WHERE row_num = 1  
						AND (
								((cs = 1 AND jzscmj > 200 AND jzscxyaqcksl <= 1) AND jsrysstd = '是') 
								OR 
								((cs >= 2 AND jzscxyaqcksl < 2 AND jzxyssltsl < 2) AND jsrysstd = '是')
						))  t1  where fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null )  ) t where 1=1  ");


                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    sbSql.Append($@" and villagedm=@villagedm ");
                    sbSqlCount.Append($@" and villagedm=@villagedm ");

                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {

                    sbSql.Append($@" and towndm=@towndm ");

                    sbSqlCount.Append($@" and towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSql.Append($@" and xzqdm=@xzqdm ");

                    sbSqlCount.Append($@" and xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbSql.Append($@" and fwbh like  @fwbh ");

                    sbSqlCount.Append($@" and fwbh  like  @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", $"%{pageRequest.fwbh}%"));
                }
                var count = _dbContext.CountClass.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();

                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

                var datas = _dbContext.MyHouseDtoYJ.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();

                var pageData = new Page<MyHouseDtoYJ>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count);

                var r = new RespRet<Page<MyHouseDtoYJ>>(true, "Ok", pageData);
                WriteSystemLog("housescj/TheBaseResultUnmatch", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<MyHouseDtoYJ>>(false, ex.ToString());
                WriteSystemLogError("housescj/TheBaseinfoUnmatch", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 房屋预警 市级和区级核验结果矛盾
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/TheBaseResultUnmatch")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<MyHouseDto>>), 200)]
        public RespRet<Page<MyHouseDtoYJREsult>> TheBaseResultUnmatch([FromBody] Fwlb pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<Page<MyHouseDtoYJREsult>>(false, "参数不正确");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<MyHouseDtoYJREsult>>(false, "user not logined");
            }
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
            var a = JsonRegion(user.District);

            try
            {
                var sbSql = new StringBuilder();
                var sbSqlCount = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                string time = DateTime.Today
                          .AddHours(23)
                          .AddMinutes(59)
                          .AddSeconds(59)
                          .ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                DateTime today = DateTime.Today;

                var prevMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);
                var year = prevMonth.Year;
                var month = prevMonth.ToString("MM");
             

                sbSql.Append($@" SELECT 
						r.xzqdm,r.xzq,r.towndm,r.town,r.villagedm,r.village, 
						r.fwbh,
						r.address,
						r.hzxm,
						 r.cs,
						r.qj_create_on,
						r.qj_jsrysstd,
						s.sj_jsrysstd,
						s.sj_create_on,
						CASE 
								WHEN r.qj_create_on > s.sj_create_on AND r.qj_jsrysstd = '否' AND s.sj_jsrysstd = '是'
								THEN '区级和市级核验结果不一致'
								WHEN s.sj_create_on > r.qj_create_on AND s.sj_jsrysstd = '否' AND r.qj_jsrysstd = '是'
								THEN '市级和区级核验结果不一致'
						END AS verification_discrepancy
				FROM 
						(SELECT
								xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								cs,
								create_on AS qj_create_on,
								jsrysstd AS qj_jsrysstd
						FROM (
								SELECT 
									m1.xzqdm, m1.xzq, m1.town,m1.towndm, m1.village, m1.villagedm, 
										m1.fwbh,
										m1.address,
										m1.hzxm,m1.cs,
										m1.create_on,
										m1.jsrysstd,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								WHERE 
										m1.yhly ='3' 
						) t
						WHERE row_num = 1) r
				LEFT JOIN 
						(	SELECT 
								fwbh,
								jsrysstd AS sj_jsrysstd,
								create_on AS sj_create_on
						FROM (
								SELECT 
										m1.fwbh,
										m1.jsrysstd,
										m1.create_on,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								WHERE 
										m1.yhly = '2'
										
						) t
						WHERE row_num = 1) s ON r.fwbh = s.fwbh
				WHERE 
						(
								(r.qj_create_on > s.sj_create_on AND r.qj_jsrysstd = '否' AND s.sj_jsrysstd = '是')
								OR 
								(s.sj_create_on > r.qj_create_on AND s.sj_jsrysstd = '否' AND r.qj_jsrysstd = '是')
						)   and r.fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null ) ");

                sbSqlCount.Append($@"  select count(1) FROM ( SELECT 
						r.xzqdm,r.xzq,r.towndm,r.town,r.villagedm,r.village, 
						r.fwbh,
						r.address,
						r.hzxm,
						 r.cs,
						r.qj_create_on,
						r.qj_jsrysstd,
						s.sj_jsrysstd,
						s.sj_create_on,
						CASE 
								WHEN r.qj_create_on > s.sj_create_on AND r.qj_jsrysstd = '否' AND s.sj_jsrysstd = '是'
								THEN '区级和市级核验结果不一致'
								WHEN s.sj_create_on > r.qj_create_on AND s.sj_jsrysstd = '否' AND r.qj_jsrysstd = '是'
								THEN '市级和区级核验结果不一致'
						END AS verification_discrepancy
				FROM 
						(SELECT
								xzqdm,xzq,town towndm,towndm town,village villagedm,villagedm village, 
								fwbh,
								address,
								hzxm,
								cs,
								create_on AS qj_create_on,
								jsrysstd AS qj_jsrysstd
						FROM (
								SELECT 
									m1.xzqdm, m1.xzq, m1.town,m1.towndm, m1.village, m1.villagedm, 
										m1.fwbh,
										m1.address,
										m1.hzxm,m1.cs,
										m1.create_on,
										m1.jsrysstd,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								WHERE 
										m1.yhly ='3' 
						) t
						WHERE row_num = 1) r
				LEFT JOIN 
						(	SELECT 
								fwbh,
								jsrysstd AS sj_jsrysstd,
								create_on AS sj_create_on
						FROM (
								SELECT 
										m1.fwbh,
										m1.jsrysstd,
										m1.create_on,
										ROW_NUMBER() OVER(PARTITION BY m1.fwbh ORDER BY m1.create_on DESC) AS row_num
								FROM 
										zm_pc_baselog_sqjz m1
								WHERE 
										m1.yhly = '2'
										
						) t
						WHERE row_num = 1) s ON r.fwbh = s.fwbh
				WHERE 
						(
								(r.qj_create_on > s.sj_create_on AND r.qj_jsrysstd = '否' AND s.sj_jsrysstd = '是')
								OR 
								(s.sj_create_on > r.qj_create_on AND s.sj_jsrysstd = '否' AND r.qj_jsrysstd = '是')
						)   and r.fwbh not in (select fwbh from  {table_name} where final_create_on='" + time + "' and final_fwzt is not null ) ) t where 1=1  ");


                if (!string.IsNullOrWhiteSpace(pageRequest.Villagedm))
                {
                    sbSql.Append($@" and r.villagedm=@villagedm ");
                    sbSqlCount.Append($@" and villagedm=@villagedm ");

                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{pageRequest.Villagedm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Towndm))
                {

                    sbSql.Append($@" and r.towndm=@towndm ");

                    sbSqlCount.Append($@" and towndm=@towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{pageRequest.Towndm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.Xzqdm))
                {
                    sbSql.Append($@" and r.xzqdm=@xzqdm ");

                    sbSqlCount.Append($@" and xzqdm=@xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{pageRequest.Xzqdm}"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbSql.Append($@" and r.fwbh like  @fwbh ");
                    sbSqlCount.Append($@" and fwbh like  @fwbh ");

                    sqlParams.Add(new NpgsqlParameter("fwbh", $"%{pageRequest.fwbh}%"));
                }

                var count = _dbContext.CountClass.FromSqlRaw(sbSqlCount.ToString(), sqlParams.ToArray()).FirstOrDefault();

                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));

                var datas = _dbContext.MyHouseDtoYJREsult.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();

                var pageData = new Page<MyHouseDtoYJREsult>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count.Count);

                var r = new RespRet<Page<MyHouseDtoYJREsult>>(true, "Ok", pageData);
                WriteSystemLog("housescj/TheBaseResultUnmatch", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<MyHouseDtoYJREsult>>(false, ex.ToString());
                WriteSystemLogError("housescj/TheBaseResultUnmatch", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 房屋隐患待督促
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-yhdx")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<PageCount<ZmPcYhLogDxDto2>>), 200)]
        public RespRet<PageCount<ZmPcYhLogDxDto2>> SearchAttrList([FromBody] PageHouseRequestInputYhdx pageRequest)
        {
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<PageCount<ZmPcYhLogDxDto2>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<PageCount<ZmPcYhLogDxDto2>>(false, "user not logined");
            }
            try
            {

                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                var len = RegionCode.Length;
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();


                string time = DateTime.Today
           .AddHours(23)
           .AddMinutes(59)
           .AddSeconds(59)
           .ToString("yyyy-MM-dd HH:mm:ss");
                sqlParams.Add(new NpgsqlParameter("time", time));

                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                sbSql.Append($@" (select '村级检查' jcly,TO_CHAR(check_time, 'YYYY-MM-DD HH24:MI:SS') checktime, a.id,a.fwbh,check_name checkname,check_nickname as checkby,hzxm,c.address FROM zm_pc_yhlog_dx_cj a,zm_user_cj b,zm_buildings c where a.fwbh=c.fwbh and b.number=a.check_by   and  a.check_by='"+userNumber+"'   and (zg_value=0 or zg_value is null ) ");
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    sbSql.Append($@" and check_time between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) ");
                    sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                    sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                }
                if (len == 6)
                {
                    sbSql.Append($@" and a.xzqdm like @xzqdm ");
                    sqlParams.Add(new NpgsqlParameter("xzqdm", $"{"%" + RegionCode + "%"}"));
                }
                if (len == 9)
                {
                    sbSql.Append($@" and a.towndm like @towndm ");
                    sqlParams.Add(new NpgsqlParameter("towndm", $"{"%" + RegionCode + "%"}"));
                }
                if (len == 12)
                {
                    sbSql.Append($@" and a.villagedm like @villagedm ");
                    sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + RegionCode + "%"}"));
                }
                //关联核查表
                sbSql.Append($@" union all select case when jcly='2' then '市级核查' when jcly='3' then '区级核查' when jcly='4' then '街镇核查' end jcly, TO_CHAR(check_time, 'YYYY-MM-DD HH24:MI:SS') checktime,a.id,a.fwbh,check_name checkname,check_nickname as checkby,hzxm,c.address FROM zm_pc_yhlog_dx_sqjz a,zm_user_cj b,zm_buildings c where a.fwbh=c.fwbh and b.number=a.check_by  and  a.check_by='"+userNumber+"'  and (zg_value=0 or zg_value is null )  ");
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    sbSql.Append($@" and check_time between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) ");
                }
                if (len == 6)
                {
                    sbSql.Append($@" and a.xzqdm like @xzqdm ");
                }
                if (len == 9)
                {
                    sbSql.Append($@" and a.towndm like @towndm ");
                }
                if (len == 12)
                {
                    sbSql.Append($@" and a.villagedm like @villagedm ");
                }
               // 关连村级base表
  //              sbSql.Append($@" union all
  //select  '村级检查', TO_CHAR(jc.create_on, 'YYYY-MM-DD HH24:MI:SS'),jc.id,b.fwbh,'未按照标准建设疏散通道',create_name,b.hzxm,b.address  from zm_pc_base_cj jc,zm_buildings b  where  jc.fwbh=b.fwbh  and jsrysstd='否'  ");
  //              if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
  //              {
  //                  sbSql.Append($@" and jc.create_on between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) ");
  //              }
  //              if (len == 6)
  //              {
  //                  sbSql.Append($@" and b.xzqdm like @xzqdm and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and xzqdm like @xzqdm and final_create_on=@time)  ");
  //              }
  //             else if (len == 9)
  //              {
  //                  sbSql.Append($@" and b.towndm like @towndm and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and towndm like @towndm and final_create_on=@time) ");
  //              }
  //             else if (len == 12)
  //              {
  //                  sbSql.Append($@" and b.villagedm like @villagedm  and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and villagedm like @villagedm and final_create_on=@time) ");
  //              }

  //              else
  //              {
  //                  sbSql.Append($@"  and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null  and final_create_on=@time) ");
  //              }
  //              //关连街镇base表
  //              sbSql.Append($@"  union all
  //select  '街镇检查', TO_CHAR(jc.create_on, 'YYYY-MM-DD HH24:MI:SS'),jc.id,b.fwbh,'未按照标准建设疏散通道',create_name,b.hzxm,b.address  from zm_pc_base_sqjz jc,zm_buildings b  where  jc.fwbh=b.fwbh and yhly='4' and fwzt is null and jsrysstd='否' and b.fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not  null)
  //");
  //              if (len == 6)
  //              {
  //                  sbSql.Append($@" and b.xzqdm like @xzqdm and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and xzqdm like @xzqdm and final_create_on=@time)  ");
  //              }
  //              else if (len == 9)
  //              {
  //                  sbSql.Append($@" and b.towndm like @towndm and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and towndm like @towndm and final_create_on=@time) ");
  //              }
  //              else if (len == 12)
  //              {
  //                  sbSql.Append($@" and b.villagedm like @villagedm  and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null and villagedm like @villagedm and final_create_on=@time) ");
  //              }


  //              else
  //              {
  //                  sbSql.Append($@"  and b.fwbh not in (select fwbh From {table_name} where  final_fwzt is not null  and final_create_on=@time) ");
  //              }
                sbSql.Append($@"   ) a ");

                sqlParams.Add(new NpgsqlParameter("yhly", user.RoleType));
                string countsql = $" select count(1) from {sbSql} ";
                //var count = _dbContext.ZmPcYhLogDxDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
                var count = 0;
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    count = _dbContext.Database.GetDbConnection().Query<int>(countsql.ToString(), new { @time = time, yhly = user.RoleType, starttime = pageRequest.CheckTime[0], endtime = pageRequest.CheckTime[1], xzqdm = "%" + RegionCode + "%", towndm = "%" + RegionCode + "%", villagedm = "%" + RegionCode + "%" }).FirstOrDefault();
                }
                else
                {
                    count = _dbContext.Database.GetDbConnection().Query<int>(countsql.ToString(), new {@time=time, yhly = user.RoleType, xzqdm = "%" + RegionCode + "%", towndm = "%" + RegionCode + "%", villagedm = "%" + RegionCode + "%" }).FirstOrDefault();
                }
                sbSql.Append($@" order by checktime desc");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (len == 6)
                {
                    sbSqlWhere.Append($@" and a.xzqdm like @xzqdm  and a.check_by='"+userNumber+"' ");
                }
                if (len == 9)
                {
                    sbSqlWhere.Append($@" and a.towndm like @towndm and a.check_by='" + userNumber + "' ");
                }
                if (len == 12)
                {
                    sbSqlWhere.Append($@" and a.villagedm like @villagedm and a.check_by='" + userNumber + "' ");
                }
                if (len == 3)
                {
                    sbSqlWhere.Append($@" and  a.check_by='" + userNumber + "' ");
                }
                var jcztsql = new StringBuilder();
                //获取各个状态的数量
                jcztsql.Append(@$"	
SELECT
(SELECT COUNT
		( CASE WHEN ( zg_value = 0 OR zg_value IS NULL ) THEN 1 END )    AS dzg 
	FROM
		( SELECT * FROM zm_pc_yhlog_dx_cj UNION ALL SELECT * FROM zm_pc_yhlog_dx_sqjz ) A 
	WHERE
		   1=1  {sbSqlWhere}
),
   COUNT(CASE WHEN jczt = '1' THEN 1 END) AS dfh,
  COUNT(CASE WHEN jczt = '2' then 1 end)AS fhtg
FROM ( SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_cj  a left join zm_pc_yhlog_dx_cj  b on a.yhlog_dx_id=b.id 
		UNION ALL 
		SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_sqjz  a left join zm_pc_yhlog_dx_sqjz  b on a.yhlog_dx_id=b.id  ) a
WHERE   1=1 {sbSqlWhere} ");
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    jcztsql = new StringBuilder();
                    jcztsql.Append(@$" SELECT
(SELECT COUNT
		( CASE WHEN ( zg_value = 0 OR zg_value IS NULL ) THEN 1 END )    AS dzg  
FROM (select * from zm_pc_yhlog_dx_cj union all select * from zm_pc_yhlog_dx_sqjz ) a
WHERE   1=1 {sbSqlWhere}
),
   COUNT(CASE WHEN jczt = '1' and CAST(zg_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) THEN 1 END) AS dfh,
  COUNT(CASE WHEN jczt = '2' and  CAST(fh_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) then 1 end)AS fhtg
FROM (SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_cj  a left join zm_pc_yhlog_dx_cj  b on a.yhlog_dx_id=b.id 
		UNION ALL 
		SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_sqjz  a left join zm_pc_yhlog_dx_sqjz  b on a.yhlog_dx_id=b.id ) a
WHERE    1=1 {sbSqlWhere}  ");
                    sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                    sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                }
;
                var num = _dbContext.Jcztnum.FromSqlRaw(jcztsql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var datas = new List<ZmPcYhLogDxDto2>();
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    datas = _dbContext.Database.GetDbConnection().Query<ZmPcYhLogDxDto2>("select * from " + sbSql.ToString(), new {time=time, yhly = user.RoleType, starttime = pageRequest.CheckTime[0], endtime = pageRequest.CheckTime[1], xzqdm = "%" + RegionCode + "%", towndm = "%" + RegionCode + "%", villagedm = "%" + RegionCode + "%", limit = pageRequest.Limit, offset = pageRequest.Limit * pageRequest.PageIndex }).ToList();
                }
                else
                {
                    datas = _dbContext.Database.GetDbConnection().Query<ZmPcYhLogDxDto2>("select * from " + sbSql.ToString(), new {time=time, yhly = user.RoleType, xzqdm = "%" + RegionCode + "%", towndm = "%" + RegionCode + "%", villagedm = "%" + RegionCode + "%", limit = pageRequest.Limit, offset = pageRequest.Limit * pageRequest.PageIndex }).ToList();
                }
                var pageData = new PageCount<ZmPcYhLogDxDto2>(datas
                   , pageRequest.PageIndex + 1, pageRequest.Limit, count, num.dzg, num.dfh, num.fhtg);
                var r = new RespRet<PageCount<ZmPcYhLogDxDto2>>(true, "Ok", pageData);
                WriteSystemLog("housescj/search-yhdx", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<PageCount<ZmPcYhLogDxDto2>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhdx", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }

        /// <summary>
        /// 房屋隐患待复核、复核通过
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-yhdxbyjczt")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<PageCount<ZmPcYhLogDxZgDto>>), 200)]
        public RespRet<PageCount<ZmPcYhLogDxZgDto>> YhdxByStatus([FromBody] PageHouseRequestInputYhdx pageRequest)
        {
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<PageCount<ZmPcYhLogDxZgDto>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<PageCount<ZmPcYhLogDxZgDto>>(false, "user not logined");
            }
            try
            {
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                var len = RegionCode.Length;
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();



                string time = DateTime.Today
           .AddHours(23)
           .AddMinutes(59)
           .AddSeconds(59)
           .ToString("yyyy-MM-dd HH:mm:ss");
                sqlParams.Add(new NpgsqlParameter("time", time));

                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;
                sbSql.Append($@" select a.create_on,a.id,a.fwbh,zg_name zg_by,hzxm,zg_time,d.check_by,c.address,a.check_name,fh_time,fh_name fh_by FROM zm_pc_yhlog_dxzgson_cj a ,zm_pc_yhlog_dx_cj d, zm_buildings c where a.fwbh=c.fwbh AND  a.yhlog_dx_id = d.ID  and check_by='"+userNumber+"' ");
                if (!string.IsNullOrWhiteSpace(pageRequest.Jczt))
                {
                    if (pageRequest.Jczt == "2")
                    {
                        sbSql = new StringBuilder();
                        sbSql.Append($@" select a.create_on,a.id,a.fwbh,zg_name zg_by,hzxm,zg_time,c.address,a.check_name,fh_time,fh_name fh_by FROM zm_pc_yhlog_dxzgson_cj a,zm_user_cj b ,zm_buildings c,zm_pc_yhlog_dx_cj d  where a.fwbh=c.fwbh and  b.number=a.fh_by  and d.id=a.yhlog_dx_id    and   d.check_by='" + userNumber + "'    ");
                        if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                        {
                            sbSql.Append($@" and CAST(fh_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone)  ");
                            sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                            sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                        }
                    }
                    if (len == 6)
                    {
                        sbSql.Append($@" and a.xzqdm like @xzqdm ");
                        sqlParams.Add(new NpgsqlParameter("xzqdm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (len == 9)
                    {
                        sbSql.Append($@" and a.towndm like @towndm ");
                        sqlParams.Add(new NpgsqlParameter("towndm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (len == 12)
                    {
                        sbSql.Append($@" and a.villagedm like @villagedm ");
                        sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (pageRequest.Jczt == "1")
                    {
                        if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                        {
                            sbSql.Append($@" and CAST(zg_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone)  ");
                            sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                            sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                        }
                    }
                    sbSql.Append($@" and a.jczt =@jczt  ");
                    sqlParams.Add(new NpgsqlParameter("jczt", $"{pageRequest.Jczt}"));
                }
                //关联核查表
                var sbSqlt = JsonConvert.DeserializeObject<string>(JsonConvert.SerializeObject(sbSql.ToString())); ;
                sbSql.Append(" union all select a.create_on,a.id,a.fwbh,zg_name zg_by,hzxm,zg_time,d.check_by,c.address,a.check_name,fh_time,fh_name fh_by FROM zm_pc_yhlog_dxzgson_sqjz a ,zm_pc_yhlog_dx_sqjz d,zm_buildings c where a.fwbh=c.fwbh and a.yhlog_dx_id=d.id  AND check_by='"+userNumber+"'"  );
                if (!string.IsNullOrWhiteSpace(pageRequest.Jczt))
                {
                    if (pageRequest.Jczt == "2")
                    {
                        sbSql = new StringBuilder();
                        sbSql.Append(sbSqlt);
                        sbSql.Append($@" union all  select a.create_on,a.id,a.fwbh,zg_name zg_by,hzxm,zg_time,c.address,a.check_name,fh_time,fh_name fh_by FROM zm_pc_yhlog_dxzgson_sqjz a,zm_user_cj b ,zm_buildings c,zm_pc_yhlog_dx_sqjz d where a.fwbh=c.fwbh and  b.number=a.fh_by and a.yhlog_dx_id=d.id  and d.check_by='"+userNumber+"'    ");
                        if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                        {
                            sbSql.Append($@" and CAST(fh_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone)  ");
                            sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                            sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                        }
                    }
                    if (len == 6)
                    {
                        sbSql.Append($@" and a.xzqdm like @xzqdm ");
                        sqlParams.Add(new NpgsqlParameter("xzqdm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (len == 9)
                    {
                        sbSql.Append($@" and a.towndm like @towndm ");
                        sqlParams.Add(new NpgsqlParameter("towndm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (len == 12)
                    {
                        sbSql.Append($@" and a.villagedm like @villagedm ");
                        sqlParams.Add(new NpgsqlParameter("villagedm", $"{"%" + RegionCode + "%"}"));
                    }
                    if (pageRequest.Jczt == "1")
                    {
                        if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                        {
                            sbSql.Append($@" and CAST(zg_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone)  ");
                            sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                            sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                        }
                    }
                    sbSql.Append($@" and a.jczt =@jczt  ");
                }

                var count = _dbContext.ZmPcYhLogDxZgDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Count();
                StringBuilder newSbSql = new StringBuilder();
                newSbSql.Append($@" select * from ( ");
                sbSql = newSbSql.Append(sbSql);
                sbSql.Append(") a");
                sbSql.Append($@" order by a.create_on desc");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (len == 6)
                {
                    sbSqlWhere.Append($@" and a.xzqdm like @xzqdm  and a.check_by='" + userNumber + "' ");
                }
                if (len == 9)
                {
                    sbSqlWhere.Append($@" and a.towndm like @towndm and a.check_by='" + userNumber + "' ");
                }
                if (len == 12)
                {
                    sbSqlWhere.Append($@" and a.villagedm like @villagedm  and a.check_by='" + userNumber + "' ");
                }
                if (len == 3)
                {
                    sbSqlWhere.Append($@"  and a.check_by='" + userNumber + "' ");
                }
                var jcztsql = new StringBuilder();
                //获取各个状态的数量
                //获取各个状态的数量
                jcztsql.Append(@$"	
SELECT
(SELECT COUNT
		( CASE WHEN ( zg_value = 0 OR zg_value IS NULL ) THEN 1 END )    AS dzg 
	FROM
		( SELECT * FROM zm_pc_yhlog_dx_cj UNION ALL SELECT * FROM zm_pc_yhlog_dx_sqjz ) A
WHERE   1=1 {sbSqlWhere}
),
   COUNT(CASE WHEN jczt = '1' THEN 1 END) AS dfh,
  COUNT(CASE WHEN jczt = '2' then 1 end)AS fhtg
FROM ( SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_cj  a left join zm_pc_yhlog_dx_cj  b on a.yhlog_dx_id=b.id 
		UNION ALL 
		SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_sqjz  a left join zm_pc_yhlog_dx_sqjz  b on a.yhlog_dx_id=b.id ) a
WHERE   1=1 {sbSqlWhere} ");
                if (pageRequest.CheckTime != null && pageRequest.CheckTime.Count() == 2)
                {
                    jcztsql = new StringBuilder();
                    jcztsql.Append(@$" SELECT
(SELECT
  COUNT(CASE WHEN (zg_value = 0 or zg_value is null ) and check_time between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone)  THEN 1 END)
  AS dzg
FROM (select * from zm_pc_yhlog_dx_cj union all select * from zm_pc_yhlog_dx_sqjz ) a
WHERE   1=1 {sbSqlWhere}
),
   COUNT(CASE WHEN jczt = '1' and CAST(zg_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) THEN 1 END) AS dfh,
  COUNT(CASE WHEN jczt = '2' and  CAST(fh_time AS timestamp without time zone) between  CAST(@starttime AS timestamp without time zone)  and CAST(@endtime AS timestamp without time zone) then 1 end)AS fhtg
FROM ( SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_cj  a left join zm_pc_yhlog_dx_cj  b on a.yhlog_dx_id=b.id 
		UNION ALL 
		SELECT a.*,b.check_by  FROM zm_pc_yhlog_dxzgson_sqjz  a left join zm_pc_yhlog_dx_sqjz  b on a.yhlog_dx_id=b.id ) a
WHERE    1=1 {sbSqlWhere}  ");
                    sqlParams.Add(new NpgsqlParameter("starttime", $"{pageRequest.CheckTime[0]}"));
                    sqlParams.Add(new NpgsqlParameter("endtime", $"{pageRequest.CheckTime[1]}"));
                }
;
                var num = _dbContext.Jcztnum.FromSqlRaw(jcztsql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                var datas = _dbContext.ZmPcYhLogDxZgDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).Select(_ => _mapper.Map<ZmPcYhLogDxZgDto>(_)).ToList();
                var pageData = new PageCount<ZmPcYhLogDxZgDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count, num.dzg, num.dfh, num.fhtg);
                var r = new RespRet<PageCount<ZmPcYhLogDxZgDto>>(true, "Ok", pageData);
                WriteSystemLog("housescj/search-yhdxbyjczt", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<PageCount<ZmPcYhLogDxZgDto>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhdxbyjczt", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }
        /// <summary>
        /// 获取隐患整改详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-yhxq")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        public RespRet<ZmPcYhLogDxDtoXq> Yhxq([FromQuery] string id)
        {
            var userNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZmPcYhLogDxDtoXq>(false, "user not logined");
                }
                if (string.IsNullOrEmpty(id))
                {
                    return new RespRet<ZmPcYhLogDxDtoXq>(false, "id不能为空");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append($@" select a.id,a.fwbh,a.check_id,a.check_name,check_zp,b.zg_value,zg_zp,a.zg_yhsyq_num,a.zg_ddc_num,check_sm  FROM zm_pc_yhlog_dxzgson_cj a,zm_pc_yhlog_dx_cj b where a.yhlog_dx_id=b.id    ");
                sbSql.Append($@" and a.id = @Id ");
                sqlParams.Add(new NpgsqlParameter("Id", $"{id}"));

                var from = _dbContext.ZmPcYhLogDxDtoXq.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (from == null)
                {
                    sbSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();
                    sbSql.Append($@" select a.id,a.fwbh,a.check_id,a.check_name,check_zp,b.zg_value,zg_zp,a.zg_yhsyq_num,a.zg_ddc_num,check_sm  FROM zm_pc_yhlog_dxzgson_sqjz a,zm_pc_yhlog_dx_sqjz b where a.yhlog_dx_id=b.id    ");
                    sbSql.Append($@" and a.id = @Id ");
                    sqlParams.Add(new NpgsqlParameter("Id", $"{id}"));
                    from = _dbContext.ZmPcYhLogDxDtoXq.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                    if (from == null)
                    {
                        var res = new RespRet<ZmPcYhLogDxDtoXq>(false, "未找到隐患信息，请传递正确ID", from);
                        WriteSystemLog("housescj/search-yhxq", res, id);
                        return res;
                    }
                }
                from.CheckZp = from.CheckZp == null ? null : string.Join("|", JsonStr2List(from.CheckZp));
                from.ZgZp = from.ZgZp == null ? null : string.Join("|", JsonStr2List(from.ZgZp));
                from.IsHaveYh = "是";

                var r = new RespRet<ZmPcYhLogDxDtoXq>(true, "Ok", from);
                WriteSystemLog("housescj/search-yhxq", r, id);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZmPcYhLogDxDtoXq>(false, ex.ToString());
                WriteSystemLogError("housescj/search-yhxq", res, id);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 复核
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-fh")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<int> Fh([FromBody] Fh model)
        {
            var userNumber = this.CurrentUser();
            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    if (string.IsNullOrEmpty(userNumber))
                    {
                        return new RespRet<int>(false, "user not logined");
                    }
                    if (model == null)
                    {
                        return new RespRet<int>(false, "请传递参数");
                    }

                    if (string.IsNullOrEmpty(model.id))
                    {
                        return new RespRet<int>(false, "id不能为空");
                    }
                    if (model.status != 0 && model.status != 1)
                    {
                        return new RespRet<int>(false, "复核只能选择通过或不通过");
                    }
                    var r = _dbContext.ZmPcYhlogDxzgsonCj.FirstOrDefault(c => c.Id == model.id);
                    if (r == null)
                    {   //从核查表里找
                        var hc = _dbContext.ZmPcYhlogDxzgsonSqjz.FirstOrDefault(c => c.Id == model.id);
                        if (hc == null)
                        {
                            var res = new RespRet<int>(false, "未找到隐患，请传递正确id");
                            WriteSystemLog("housescj/search-fh", res, model);
                            return res;
                        }
                        var userhc = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
                        if (userhc == null)
                        {
                            var res = new RespRet<int>(false, "未找用户信息");
                            WriteSystemLog("housescj/search-fh", res, model);
                            return res;
                        }
                        var dxhc = _dbContext.ZmPcYhLogDxSqjz.FirstOrDefault(c => c.Id == hc.YhlogDxId);
                        if (dxhc != null)
                        {
                            hc.FhZt = model.status;
                            hc.FhSm = model.fhsm;
                            hc.FhTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            hc.FhBy = userNumber;
                            hc.FhName = userhc.Nickname;
                            //修改单项隐患表
                            dxhc.FhZt = model.status;
                            dxhc.FhTimeDx = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            dxhc.FhBYDx = userNumber;
                            dxhc.FhNameDx= userhc.Nickname;

                            if (model.status == 0)
                            {
                                dxhc.Jczt = "2";
                                hc.Jczt = "2";
                                var yh = _dbContext.ZmPcYhLogDxSqjz.Where(c => c.Fwbh == hc.Fwbh && c.Id != hc.YhlogDxId && c.FhZt != 0 && c.YhlogId == dxhc.YhlogId).ToList();
                                if (yh == null || yh.Count() == 0)
                                {
                                    var jclog = _dbContext.ZmPcYhSqjz.FirstOrDefault(c => c.Fwbh == hc.Fwbh);
                                    if (jclog != null)
                                    {
                                        jclog.Pczt = 1;
                                        _dbContext.Update(jclog);
                                        if (_dbContext.SaveChanges() == 0)
                                        {
                                            WriteSystemLog("housescj/search-fh", new RespRet<int>(false, "修改村级隐患表出错", 0), model);
                                            return new RespRet<int>(false, "复核失败", 0);
                                        }
                                    }

                                }
                            }
                            else
                            {
                                dxhc.ZgValue = 0;
                                dxhc.Jczt = null;
                                hc.Jczt = null;
                            }
                            _dbContext.Update(hc);
                            _dbContext.Update(dxhc);
                            if (_dbContext.SaveChanges() == 2)
                            {
                                transaction.Commit();
                                var res = new RespRet<int>(true, "操作成功");
                                WriteSystemLog("housescj/search-fh", res, model);
                                return res;
                            }
                            else
                            {
                                transaction.Rollback();
                                var res = new RespRet<int>(false, "操作失败");
                                WriteSystemLog("housescj/search-fh", res, model);
                                return res;
                            }
                        }
                        else
                        {
                            var res = new RespRet<int>(false, "未找到隐患，请传递正确id");
                            WriteSystemLog("housescj/search-fh", res, model);
                            return res;
                        }

                    }
                    var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
                    if (user == null)
                    {
                        var res = new RespRet<int>(false, "未找用户信息");
                        WriteSystemLog("housescj/search-fh", res, model);
                        return res;
                    }
                    var dx = _dbContext.ZmPcYhLogDxCj.FirstOrDefault(c => c.Id == r.YhlogDxId);
                    if (dx != null)
                    {
                        r.FhZt = model.status;
                        r.FhSm = model.fhsm;
                        r.FhTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        r.FhBy = userNumber;
                        r.FhName = user.Nickname;
                        //修改单项隐患表
                        dx.FhZt = model.status;
                        dx.FhTimeDx = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        dx.FhBYDx = userNumber;
                        dx.FhNameDx = user.Nickname;

                        if (model.status == 0)
                        {
                            dx.Jczt = "2";
                            r.Jczt = "2";
                            var yh = _dbContext.ZmPcYhLogDxCj.Where(c => c.Fwbh == r.Fwbh && c.Id != r.YhlogDxId && c.FhZt != 0 && c.YhlogId == dx.YhlogId).ToList();
                            if (yh == null || yh.Count == 0)
                            {
                                var jclog = _dbContext.ZmPcYhCj.FirstOrDefault(c => c.Fwbh == r.Fwbh);
                                if (jclog != null)
                                {
                                    jclog.Pczt = 1;
                                    _dbContext.Update(jclog);
                                    if (_dbContext.SaveChanges() == 0)
                                    {
                                        WriteSystemLog("housescj/search-fh", new RespRet<int>(false, "修改ZmPcYhCj出错", 0), model);
                                        return new RespRet<int>(false, "复核失败", 0);
                                    }
                                }

                            }
                        }
                        else
                        {
                            dx.ZgValue = 0;
                            dx.Jczt = null;
                            r.Jczt = null;
                        }
                        _dbContext.Update(r);
                        _dbContext.Update(dx);
                        if (_dbContext.SaveChanges() == 2)
                        {
                            transaction.Commit();
                            var res = new RespRet<int>(true, "操作成功");
                            WriteSystemLog("housescj/search-fh", res, model);
                            return res;
                        }
                        else
                        {
                            transaction.Rollback();
                            var res = new RespRet<int>(false, "操作失败");
                            WriteSystemLog("housescj/search-fh", res, model);
                            return res;
                        }
                    }
                    else
                    {
                        var res = new RespRet<int>(false, "未找到隐患，请传递正确id");
                        WriteSystemLog("housescj/search-fh", res, model);
                        return res;
                    }
                }
                catch (Exception ex)
                {
                    var res = new RespRet<int>(false, ex.ToString());
                    WriteSystemLogError("housescj/search-fh", res, model);
                    res.Msg = "系统异常";
                    return res;
                }
            }
        }
        ///// <summary>
        ///// 获取复核、整改详情
        ///// </summary>
        ///// <param name="pageRequest"></param>
        ///// <returns></returns>
        //[HttpPost("housescj/search-fhzgxq")]
        //[Produces("application/json")]
        //[ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        //public RespRet<ZmPcYhLogDxZgDtoCj> Fhzgxq([FromQuery] string id)
        //{
        //    var userNumber = this.CurrentUser();
        //    try
        //    {
        //        if (string.IsNullOrEmpty(userNumber))
        //        {
        //            return new RespRet<ZmPcYhLogDxZgDtoCj>(false, "user not logined");
        //        }
        //        if (string.IsNullOrWhiteSpace(id))
        //        {
        //            return new RespRet<ZmPcYhLogDxZgDtoCj>(false, "隐患id不能为空");
        //        }
        //        var sbSql = new StringBuilder();
        //        var sbSqlWhere = new StringBuilder();
        //        var sqlParams = new List<NpgsqlParameter>();
        //        sbSql.Append($@"select *  FROM zm_pc_yhlog_dxzgson_cj    ");
        //        sbSqlWhere.Append($@" id = @Id and");
        //        sqlParams.Add(new NpgsqlParameter("Id", $"{id}"));

        //        if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
        //        {
        //            sbSql.Append($@" where {sbSqlWhere} 1=1 ");
        //        }
        //        var from = _dbContext.ZmPcYhLogDxZgDtoCj.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
        //        if (from == null)
        //        {
        //            var res = new RespRet<ZmPcYhLogDxZgDtoCj>(false, "未找到隐患详情", from);
        //            WriteSystemLog("housescj/search-yhxq", res);
        //            return res;
        //        }
        //        var r = new RespRet<ZmPcYhLogDxZgDtoCj>(true, "Ok", from);
        //        WriteSystemLog("housescj/search-yhxq", r);
        //        return r;
        //    }
        //    catch (Exception ex)
        //    {
        //        var res = new RespRet<ZmPcYhLogDxZgDtoCj>(false, ex.ToString());
        //        WriteSystemLogError("housescj/search-yhxq", res);
        //        res.Msg = "系统异常";
        //        return res;
        //    }
        //}


        /// <summary>
        /// 对隐患数据进行整改
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/update-yhzg")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        public RespRet<int> Yhzg([FromBody] ZmPcYhlogDxzgson model)
        {

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                var userNumber = this.CurrentUser();
                try
                {
                    if (string.IsNullOrEmpty(userNumber))
                    {
                        return new RespRet<int>(false, "user not logined");
                    }
                    var yh = _dbContext.ZmPcYhLogDxCj.FirstOrDefault(c => c.Id == model.Id);
                    if (yh != null)
                    {
                        if (yh.ZgValue == 1)
                        {
                            transaction.Rollback();
                            var r = new RespRet<int>(false, "该隐患已整改，无需重复操作");
                            WriteSystemLog("housescj/search-yhzg", r, model);
                            return r;
                        }

                        //村级检查隐患
                        yh.ZgValue = 1;
                        _dbContext.Update(yh);
                        var res = _dbContext.SaveChanges();
                        if (res > 0)
                        {
                            var add = _mapper.Map<ZmPcYhlogDxzgsonCj>(yh);
                            add.ZgZp = model.ZgZp == null ? null : JsonConvert.SerializeObject(model.ZgZp.Split("|"));
                            add.ZgDdcNum = model.ZgDdcNum;
                            add.ZgYhsyqNum = model.ZgYhsyqNum;
                            add.Jczt = "1";
                            _dbContext.Add(add);
                            int i = _dbContext.SaveChanges();
                            if (i > 0)
                            {
                                transaction.Commit();
                                var r = new RespRet<int>(true, "Ok", i);
                                WriteSystemLog("housescj/search-yhzg", r, model);
                                return r;
                            }
                            else
                            {
                                transaction.Rollback();
                                var r = new RespRet<int>(false, "操作失败");
                                WriteSystemLog("housescj/search-yhzg", r, model);
                                return r;
                            }
                        }
                        else
                        {
                            transaction.Rollback();
                            var r = new RespRet<int>(false, "操作失败");
                            WriteSystemLog("housescj/search-yhzg", r, model);
                            return r;
                        }
                    }
                    //从村民找
                    var yhcm = _dbContext.ZmPcYhLogDx.FirstOrDefault(c => c.Id == model.Id);
                    if (yhcm != null)
                    {
                        if (yhcm.ZgValue == 1)
                        {
                            transaction.Rollback();
                            var r = new RespRet<int>(false, "该隐患已整改，无需重复操作");
                            WriteSystemLog("housescj/search-yhzg", r, model);
                            return r;
                        }
                        yhcm.ZgValue = 1;
                        _dbContext.Update(yhcm);
                        var res = _dbContext.SaveChanges();
                        if (res > 0)
                        {
                            var add = _mapper.Map<ZmPcYhlogDxzgson>(yhcm);
                            add.ZgZp = model.ZgZp == null ? null : JsonConvert.SerializeObject(model.ZgZp.Split("|"));
                            add.ZgDdcNum = model.ZgDdcNum;
                            add.ZgYhsyqNum = model.ZgYhsyqNum;
                            _dbContext.Add(add);
                            int i = _dbContext.SaveChanges();
                            if (i > 0)
                            {
                                transaction.Commit();
                                var r = new RespRet<int>(true, "Ok", i);
                                WriteSystemLog("housescj/search-yhzg", r, model);
                                return r;
                            }
                            else
                            {
                                transaction.Rollback();
                                var r = new RespRet<int>(false, "操作失败");
                                WriteSystemLog("housescj/search-yhzg", r, model);
                                return r;
                            }
                        }
                        else
                        {
                            transaction.Rollback();
                            var r = new RespRet<int>(false, "操作失败");
                            WriteSystemLog("housescj/search-yhzg", r, model);
                            return r;
                        }
                    }
                    else
                    {
                        transaction.Rollback();
                        var r = new RespRet<int>(false, "未找到该隐患，请刷新重试");
                        WriteSystemLog("housescj/search-yhzg", r, model);
                        return r;
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    var res = new RespRet<int>(false, ex.ToString());
                    WriteSystemLogError("housescj/search-yhzg", res, model);
                    res.Msg = "系统异常";
                    return res;
                }
            }
        }
        /// <summary>
        /// 获取隐患详情
        /// </summary>
        /// <param name = "pageRequest" ></ param >
        /// < returns ></ returns >
        //[HttpPost("housescj/search-yhxq")]
        //[Produces("application/json")]
        //[ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        //public RespRet<ZmPcYhLogDxDtoXq> Yhxq([FromQuery] string id)
        //{
        //    var userNumber = this.CurrentUser();
        //    try
        //    {

        //        if (string.IsNullOrEmpty(userNumber))
        //        {
        //            return new RespRet<ZmPcYhLogDxDtoXq>(false, "user not logined");
        //        }

        //        var sbSql = new StringBuilder();
        //        var sbSqlWhere = new StringBuilder();
        //        var sqlParams = new List<NpgsqlParameter>();

        //        sbSql.Append($@"select id,fwbh,jcly,check_id,check_name,check_sm,check_zp,zg_value  FROM zm_pc_yhlog_dx    ");


        //        sbSqlWhere.Append($@" id = @Id and");

        //        sqlParams.Add(new NpgsqlParameter("Id", $"{id}"));

        //        if (!string.IsNullOrEmpty(sbSqlWhere.ToString()))
        //        {
        //            sbSql.Append($@" where {sbSqlWhere} 1=1 ");
        //        }
        //        var from = _dbContext.ZmPcYhLogDxDtoXq.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).FirstOrDefault();
        //        if (from == null)
        //        {
        //            var res = new RespRet<ZmPcYhLogDxDtoXq>(false, "未找到隐患信息，请传递正确ID", from);
        //            WriteSystemLog("housescj/search-yhxq", res);
        //            return res;
        //        }
        //        from.CheckZp = from.CheckZp == null ? null : string.Join("|", JsonStr2List(from.CheckZp));
        //        from.IsHaveYh = from.ZgValue == 0 ? "是" : "否";

        //        var r = new RespRet<ZmPcYhLogDxDtoXq>(true, "Ok", from);
        //        WriteSystemLog("housescj/search-yhxq", r);
        //        return r;
        //    }
        //    catch (Exception ex)
        //    {
        //        var res = new RespRet<ZmPcYhLogDxDtoXq>(false, ex.ToString());
        //        WriteSystemLogError("housescj/search-yhxq", res);
        //        res.Msg = "系统异常";
        //        return res;
        //    }
        //}


        /// <summary>
        /// 房屋信息填报
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/update-yhzc")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<int> UpdateHouseByFwbh([FromBody] UpdateHouseCjDto from)
        {
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<int>(false, "user not logined");
            }
            if (from == null || from.ZmPcBase == null)
            {
                return new RespRet<int>(false, "请正确传递参数");
            }
            if (from.ZmPcYh != null)
            {
                from.ZmPcYh.Pczt = 0;
            }
            try
            {
                var basemsg2 = _dbContext.ZmBuildings.FirstOrDefault(u => u.Fwbh == from.ZmPcBase.Fwbh);
                if (basemsg2 == null)
                {
                    var r1 = new RespRet<int>(false, "房屋不存在");
                    WriteSystemLog("housescj/update-yhzc", r1, from);
                    return r1;
                }
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
                var id = Guid.NewGuid().ToString();
                var pc = _dbContext.ZmPcBaseCj.AsNoTracking().FirstOrDefault(u => u.Fwbh == from.ZmPcBase.Fwbh && u.IsDelete != 1);
                //之前没有采集过，设置默认信息
                from.ZmPcBase.CreateBy = user.Number;
                from.ZmPcBase.CreateName = user.Nickname;
                from.ZmPcBase.CreateOn = DateTime.Now;
                from.ZmPcBase.CreateIp = this.IpAddress();
                from.ZmPcBase.IsDelete = null;
                from.ZmPcBase.DeleteBy = null;
                from.ZmPcBase.DeleteOn = null;
                from.ZmPcBase.DeleteIp = null;
                from.ZmPcBase.Id = id;
                from.ZmPcBase.Fwbh = from.ZmPcBase.Fwbh;
                from.ZmPcBase.Hzxm = basemsg2.Hzxm;
                from.ZmPcBase.Yhly = user.RoleType;
                from.ZmPcBase.Xzqdm = basemsg2.Xzqdm;
                from.ZmPcBase.Town = basemsg2.Towndm;
                from.ZmPcBase.Village = basemsg2.Villagedm;
                from.ZmPcBase.Xzq = basemsg2.Xzq;
                from.ZmPcBase.Towndm = basemsg2.Town;
                from.ZmPcBase.Villagedm = basemsg2.Village;
                from.ZmPcBase.CreateName = user.Nickname;
                from.ZmPcBase.Address = basemsg2.Address;
                from.ZmPcBase.Pcsj = DateTime.Now;
                from.ZmPcBase.SfmzstdPhoto = from.ZmPcBase.SfmzstdPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcBase.SfmzstdPhoto.Split("|"));
                from.ZmPcBase.SfmzstdZgPhoto = from.ZmPcBase.SfmzstdZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcBase.SfmzstdZgPhoto.Split("|"));


                from.ZmPcBase.SfczjxgbczzrPhoto = (from.ZmPcBase.SfczjxgbczzrPhoto == null || from.ZmPcBase.SfczjxgbczzrPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.SfczjxgbczzrPhoto.Split("|"));
                from.ZmPcBase.SfczjxgbczzrZgPhoto = (from.ZmPcBase.SfczjxgbczzrZgPhoto == null || from.ZmPcBase.SfczjxgbczzrZgPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.SfczjxgbczzrZgPhoto.Split("|"));


                from.ZmPcBase.SstdsyyrkrclzxPhoto = (from.ZmPcBase.SstdsyyrkrclzxPhoto == null || from.ZmPcBase.SstdsyyrkrclzxPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.SstdsyyrkrclzxPhoto.Split("|"));
                from.ZmPcBase.SstdsyyrkrclzxZgPhoto = (from.ZmPcBase.SstdsyyrkrclzxZgPhoto == null || from.ZmPcBase.SstdsyyrkrclzxZgPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.SstdsyyrkrclzxZgPhoto.Split("|"));




                from.ZmPcBase.GryssdcsszblyyqksddpPhoto = (from.ZmPcBase.GryssdcsszblyyqksddpPhoto == null || from.ZmPcBase.GryssdcsszblyyqksddpPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.GryssdcsszblyyqksddpPhoto.Split("|"));
                from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto = (from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto == null || from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto.Split("|"));



                from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto = (from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto == null || from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto.Split("|"));
                from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto= (from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto == null || from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto == "") ? null : JsonConvert.SerializeObject(from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto.Split("|"));


            



                //隐患识别与数据校验
                var yhlog_id = Guid.NewGuid().ToString();
                var list = new List<ZmPcYhLogDxCj>();
                var listson = new List<ZmPcYhlogDxzgsonCj>();

                if (from.ZmPcBase != null)
                {
                    //彩钢板建筑是否出租住人    wdwhjgfjgqsyyrkrgbdj = 1   THEN '存在彩钢板建筑出租住人'
                    if (from.ZmPcBase.Wdwhjgfjgqsyyrkrgbdj == 1 || from.ZmPcBase.Wdwhjgfjgqsyyrkrgbdj == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcBase.Wdwhjgfjgqsyyrkrgbdj == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcBase.SfczjxgbczzrPhoto))
                            {
                                return new RespRet<int>(false, "存在夹芯彩钢板出租住人照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcBase.Fwbh,
                                Id = ddcid,
                                YhlogId = from.ZmPcBase.Id, //这个需要确认  挂接base的
                                Jcly = user.RoleType,
                                CheckId = "wdwhjgfjgqsyyrkrgbdj",
                                CheckName = "存在夹芯彩钢板出租住人",
                                CheckSm = from.ZmPcBase.SfczjxgbczzrMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcBase.SfczjxgbczzrPhoto,
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcBase.SfczjxgbczzrZgqk == null ? 0 : from.ZmPcBase.SfczjxgbczzrZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname
                            });
                            //如果整改
                            if (from.ZmPcBase.SfczjxgbczzrZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcBase.SfczjxgbczzrZgPhoto))
                                {
                                    return new RespRet<int>(false, "存在夹芯彩钢板出租住人的整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "wdwhjgfjgqsyyrkrgbdj",
                                    CheckName = "存在夹芯彩钢板出租住人",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,  //隐患的id
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString(),
                                    ZgZp = from.ZmPcBase.SfczjxgbczzrZgPhoto,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }

                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "是否存在夹芯彩钢板出租住人只能为1或0");
                    }
                    //建设人员疏散通道     jsrysstd = 是   THEN '是否按标准疏散通道'
                    if (from.ZmPcBase.Jsrysstd == "是" || from.ZmPcBase.Jsrysstd == "否")
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcBase.Jsrysstd == "否")
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcBase.SfmzstdPhoto))
                            {
                                return new RespRet<int>(false, "是否按标准建设疏散通道照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcBase.Fwbh,
                                Id = ddcid,
                                YhlogId = from.ZmPcBase.Id, //这个需要确认  挂接base的
                                Jcly = user.RoleType,
                                CheckId = "jsrysstd",
                                CheckName = "未按标准建设疏散通道",
                                CheckSm = from.ZmPcBase.SfmzstdMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcBase.SfmzstdPhoto,
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcBase.SfmzstdZgqk == null ? 0 : from.ZmPcBase.SfmzstdZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname

                            });
                            //如果整改
                            if (from.ZmPcBase.SfmzstdZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcBase.SfmzstdZgPhoto))
                                {
                                    return new RespRet<int>(false, "是否按标准建设疏散通道整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "jsrysstd",
                                    CheckName = "未按标准建设疏散通道",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,  //隐患的id
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString(),
                                    ZgZp = from.ZmPcBase.SfmzstdZgPhoto,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }

                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "是否按标准疏散通道只能为是或否");
                    }


                    //疏散通道使用易燃可燃材料装修（1是0否）   sstdsyyrkrclzx
                    if (from.ZmPcBase.Sstdsyyrkrclzx == 1 || from.ZmPcBase.Sstdsyyrkrclzx == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcBase.Sstdsyyrkrclzx == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcBase.SstdsyyrkrclzxPhoto))
                            {
                                return new RespRet<int>(false, "疏散通道使用易燃可燃材料装修照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcBase.Fwbh,
                                Id = ddcid,
                                YhlogId = from.ZmPcBase.Id, //这个需要确认  挂接base的
                                Jcly = user.RoleType,
                                CheckId = "sstdsyyrkrclzx",
                                CheckName = "疏散通道使用易燃可燃材料装修",
                                CheckSm = from.ZmPcBase.SstdsyyrkrclzxMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcBase.SstdsyyrkrclzxPhoto,
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcBase.SstdsyyrkrclzxZgqk == null ? 0 : from.ZmPcBase.SstdsyyrkrclzxZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname

                            });
                            //如果整改
                            if (from.ZmPcBase.SstdsyyrkrclzxZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcBase.SstdsyyrkrclzxZgPhoto))
                                {
                                    return new RespRet<int>(false, "疏散通道使用易燃可燃材料装修的整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "sstdsyyrkrclzx",
                                    CheckName = "疏散通道使用易燃可燃材料装修",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,  //隐患的id
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString(),
                                    ZgZp = from.ZmPcBase.SstdsyyrkrclzxZgPhoto,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }

                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "疏散通道使用易燃可燃材料装修只能为1或0");
                    }


                    //供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚     Gryssdcsszblyyqksddp = 1   THEN '顶棚南棚'
                    if (from.ZmPcBase.Gryssdcsszblyyqksddp == 1 || from.ZmPcBase.Gryssdcsszblyyqksddp == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcBase.Gryssdcsszblyyqksddp == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcBase.GryssdcsszblyyqksddpPhoto))
                            {
                                return new RespRet<int>(false, "供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcBase.Fwbh,
                                Id = ddcid,
                                YhlogId = from.ZmPcBase.Id, //这个需要确认  挂接base的
                                Jcly = user.RoleType,
                                CheckId = "gryssdcsszblyyqksddp",
                                CheckName = "供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚",
                                CheckSm = from.ZmPcBase.GryssdcsszblyyqksddpMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcBase.GryssdcsszblyyqksddpPhoto,
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcBase.GryssdcsszblyyqksddpZgqk == null ? 0 : from.ZmPcBase.GryssdcsszblyyqksddpZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname

                            });
                            //如果整改
                            if (from.ZmPcBase.GryssdcsszblyyqksddpZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto))
                                {
                                    return new RespRet<int>(false, "供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚的整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "gryssdcsszblyyqksddp",
                                    CheckName = "供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,  //隐患的id
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString(),
                                    ZgZp = from.ZmPcBase.GryssdcsszblyyqksddpZgPhoto,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }

                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "供人员疏散的场院、内院、天井设置不利于烟气扩散的顶棚南棚只能为1或0");
                    }

                    //服务充电设施服务于房屋的电动自行车充电设施是否按要求建设和收费  是 1是0否2不涉及 
                    if (from.ZmPcBase.Fwyfwdddzxccdsssfayqjshsf == 1 || from.ZmPcBase.Fwyfwdddzxccdsssfayqjshsf == 0 || from.ZmPcBase.Fwyfwdddzxccdsssfayqjshsf == 2)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcBase.Fwyfwdddzxccdsssfayqjshsf == 0)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto))
                            {
                                return new RespRet<int>(false, "服务于房屋的电动自行车充电设施未按要求建设和收费照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcBase.Fwbh,
                                Id = ddcid,
                                YhlogId = from.ZmPcBase.Id, //这个需要确认  挂接base的
                                Jcly = user.RoleType,
                                CheckId = "fwyfwdddzxccdsssfayqjshsf",
                                CheckName = "服务于房屋的电动自行车充电设施未按要求建设和收费",
                                CheckSm = from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfPhoto,
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgqk == null ? 0 : from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname

                            });
                            //如果整改
                            if (from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto))
                                {
                                    return new RespRet<int>(false, "服务于房屋的电动自行车充电设施未按要求建设和收费d的整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "fwyfwdddzxccdsssfayqjshsf",
                                    CheckName = "服务于房屋的电动自行车充电设施未按要求建设和收费",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,  //隐患的id
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString(),
                                    ZgZp = from.ZmPcBase.FwyfwdddzxccdsssfayqjshsfZgPhoto,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }

                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "服务于房屋的电动自行车充电设施是否按要求建设和收费只能为1或0或2");
                    }
                }


                if (from.ZmPcYh != null)
                {
                    //参数验证
                    //电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电(1是0否)
                    if (from.ZmPcYh.UseElectricBicycle == 1 || from.ZmPcYh.UseElectricBicycle == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcYh.UseElectricBicycle == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseElectricBicyclePhoto))
                            {
                                return new RespRet<int>(false, "电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电的照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            //整改了也需要往数据库插数据，标记已整改
                            var ddcid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcYh.Fwbh,
                                Id = ddcid,
                                YhlogId = yhlog_id,
                                Jcly = user.RoleType,
                                CheckId = "use_electric_bicycle",
                                CheckName = "电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电",
                                CheckSm = from.ZmPcYh.UseElectricBicycleMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcYh.UseElectricBicyclePhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseElectricBicyclePhoto.Split("|")),
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname
                            });
                            //如果整改
                            if (from.ZmPcYh.UseElectricBicycleZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseElectricBicycleZgPhoto))
                                {
                                    return new RespRet<int>(false, "电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电的整改照片不能为空");
                                }
                                if (from.ZmPcYh.Zxqlddcsl <= 0)
                                {
                                    return new RespRet<int>(false, "自行清理电动车数量必须大于0");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "use_electric_bicycle",
                                    CheckName = "电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = ddcid,
                                    ZgBy = basemsg2.Hzxm,
                                    ZgDdcNum = from.ZmPcYh.Zxqlddcsl,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    ZgZp = from.ZmPcYh.UseElectricBicycleZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseElectricBicycleZgPhoto.Split("|")),
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "电动自行车、电动平衡车及其蓄电池在室内及安全出口处停放、充电的值只能为1或0");
                    }
                    //参数验证
                    // 在公共区域、租住房间内、地下室使用瓶装液化石油气(1是0否)
                    if (from.ZmPcYh.UseBottledLiquefiedGas == 1 || from.ZmPcYh.UseBottledLiquefiedGas == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcYh.UseBottledLiquefiedGas == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseBottledLiquefiedGasPhoto))
                            {
                                return new RespRet<int>(false, " 在公共区域、租住房间内、地下室使用瓶装液化石油气的照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            var czfwid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcYh.Fwbh,
                                Id = czfwid,
                                YhlogId = yhlog_id,
                                Jcly = user.RoleType,
                                CheckId = "use_bottled_liquefied_gas",
                                CheckName = " 在公共区域、租住房间内、地下室使用瓶装液化石油气",
                                CheckSm = from.ZmPcYh.UseBottledLiquefiedGasMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcYh.UseBottledLiquefiedGasPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseBottledLiquefiedGasPhoto.Split("|")),
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname,
                            });
                            //如果整改
                            if (from.ZmPcYh.UseBottledLiquefiedGasZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseBottledLiquefiedGasZgPhoto))
                                {
                                    return new RespRet<int>(false, " 在公共区域、租住房间内、地下室使用瓶装液化石油气整改照片不能为空");
                                }
                                if (from.ZmPcYh.Zxqlyhsyqsl <= 0)
                                {
                                    return new RespRet<int>(false, "自行清理液化石油气数量必须大于0");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "use_bottled_liquefied_gas",
                                    CheckName = " 在公共区域、租住房间内、地下室使用瓶装液化石油气",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = czfwid,
                                    ZgBy = basemsg2.Hzxm,
                                    ZgYhsyqNum = from.ZmPcYh.Zxqlyhsyqsl,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    ZgZp = from.ZmPcYh.UseBottledLiquefiedGasZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseBottledLiquefiedGasZgPhoto.Split("|")),
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                });
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, " 在公共区域、租住房间内、地下室使用瓶装液化石油气的值只能为1或0");
                    }
                    //参数验证
                    //疏散走道、楼梯间、安全出口处堆放物品影响人员疏散(1是0否)
                    if (from.ZmPcYh.UseStackedItems == 1 || from.ZmPcYh.UseStackedItems == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcYh.UseStackedItems == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseStackedItemsPhoto))
                            {
                                return new RespRet<int>(false, "疏散走道、楼梯间、安全出口处堆放物品影响人员疏散的照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            var sszdid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcYh.Fwbh,
                                Id = sszdid,
                                YhlogId = yhlog_id,
                                Jcly = user.RoleType,
                                CheckId = "use_stacked_items",
                                CheckName = "疏散走道、楼梯间、安全出口处堆放物品影响人员疏散",
                                CheckSm = from.ZmPcYh.UseStackedItemsMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcYh.UseStackedItemsPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseStackedItemsPhoto.Split("|")),
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname
                            });
                            //如果整改
                            if (from.ZmPcYh.UseStackedItemsZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.UseStackedItemsZgPhoto))
                                {
                                    return new RespRet<int>(false, "疏散走道、楼梯间、安全出口处堆放物品影响人员疏散整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "use_stacked_items",
                                    CheckName = "疏散走道、楼梯间、安全出口处堆放物品影响人员疏散",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = sszdid,
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    ZgZp = from.ZmPcYh.UseStackedItemsZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseStackedItemsZgPhoto.Split("|")),
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm,

                                }); ;
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "疏散走道、楼梯间、安全出口处堆放物品影响人员疏散的值只能为1或0");
                    }
                    //参数验证
                    //灭火器未保持完好有效(1是0否)
                    if (from.ZmPcYh.Mhqwbcwhyx == 1 || from.ZmPcYh.Mhqwbcwhyx == 0)
                    {
                        //如果等于1，描述不能为空
                        if (from.ZmPcYh.Mhqwbcwhyx == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcYh.MhqwbcwhyxPhoto))
                            {
                                return new RespRet<int>(false, "灭火器未保持完好有效的照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            var mhqid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcYh.Fwbh,
                                Id = mhqid,
                                YhlogId = yhlog_id,
                                Jcly = user.RoleType,
                                CheckId = "mhqwbcwhyx",
                                CheckName = "灭火器未保持完好有效",
                                CheckSm = from.ZmPcYh.MhqwbcwhyxMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcYh.MhqwbcwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.MhqwbcwhyxPhoto.Split("|")),
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname
                            });
                            //如果整改
                            if (from.ZmPcYh.MhqwbcwhyxZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.MhqwbcwhyxZgPhoto))
                                {
                                    return new RespRet<int>(false, "灭火器未保持完好有效整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "mhqwbcwhyx",
                                    CheckName = "灭火器未保持完好有效",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = mhqid,
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    ZgZp = from.ZmPcYh.MhqwbcwhyxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.MhqwbcwhyxZgPhoto.Split("|")),
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                }); ;
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "灭火器未保持完好有效的值只能为1或0");
                    }
                    //参数验证
                    //私拉乱接电线（1是0否）
                    if (from.ZmPcYh.Slljdx == 1 || from.ZmPcYh.Slljdx == 0)
                    {

                        //如果等于1，描述不能为空
                        if (from.ZmPcYh.Slljdx == 1)
                        {
                            if (string.IsNullOrWhiteSpace(from.ZmPcYh.SlljdxPhoto))
                            {
                                return new RespRet<int>(false, "私拉乱接电线的照片不能为空");
                            }
                            from.ZmPcYh.Pczt += 1;
                            var slljdxid = Guid.NewGuid().ToString();
                            list.Add(new ZmPcYhLogDxCj
                            {
                                Fwbh = from.ZmPcYh.Fwbh,
                                Id = slljdxid,
                                YhlogId = yhlog_id,
                                Jcly = user.RoleType,
                                CheckId = "slljdx",
                                CheckName = "私拉乱接电线",
                                CheckSm = from.ZmPcYh.SlljdxMsg,
                                CheckBy = userNumber,
                                CheckTime = DateTime.Now,
                                CheckZp = from.ZmPcYh.SlljdxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.SlljdxPhoto.Split("|")),
                                CreateOn = DateTime.Now,
                                ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                Xzq = basemsg2.Xzq,
                                Xzqdm = basemsg2.Xzqdm,
                                Town = basemsg2.Town,
                                Towndm = basemsg2.Towndm,
                                Village = basemsg2.Village,
                                Villagedm = basemsg2.Villagedm,
                                CheckNickname = user.Nickname
                            });
                            //如果整改
                            if (from.ZmPcYh.SlljdxZgqk == 1)
                            {
                                from.ZmPcYh.Pczt -= 1;
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.SlljdxZgPhoto))
                                {
                                    return new RespRet<int>(false, "私拉乱接电线整改照片不能为空");
                                }
                                listson.Add(new ZmPcYhlogDxzgsonCj
                                {
                                    CheckId = "slljdx",
                                    CheckName = "私拉乱接电线",
                                    CreateOn = DateTime.Now,
                                    Fwbh = basemsg2.Fwbh,
                                    Id = Guid.NewGuid().ToString(),
                                    YhlogDxId = slljdxid,
                                    ZgBy = basemsg2.Hzxm,
                                    Zglx = "2",
                                    ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    ZgZp = from.ZmPcYh.SlljdxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.SlljdxZgPhoto.Split("|")),
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm
                                }); ;
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "私拉乱接电线的值只能为1或0");
                    }
                    //参数验证
                    //简易喷淋灭火系统未保持完好有效(1是0否2不涉及)
                    if (from.ZmPcYh.Jyplmhxtwbcwhyx == 1 || from.ZmPcYh.Jyplmhxtwbcwhyx == 0 || from.ZmPcYh.Jyplmhxtwbcwhyx == 2)
                    {
                        if (from.ZmPcYh.Jyplmhxtwbcwhyx == 2)
                        {

                        }
                        else
                        {
                            //如果等于1，描述不能为空
                            if (from.ZmPcYh.Jyplmhxtwbcwhyx == 1)
                            {
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.JyplmhxtwbcwhyxPhoto))
                                {
                                    return new RespRet<int>(false, "简易喷淋灭火系统未保持完好有效的照片不能为空");
                                }
                                from.ZmPcYh.Pczt += 1;
                                //if (string.IsNullOrWhiteSpace(from.JyplmhxtwbcwhyxMsg))
                                //{
                                //    return new RespRet<int>(false, "简易喷淋灭火系统未保持完好有效的描述不能为空");
                                //}
                                var jyplhid = Guid.NewGuid().ToString();
                                list.Add(new ZmPcYhLogDxCj
                                {
                                    Fwbh = from.ZmPcYh.Fwbh,
                                    Id = jyplhid,
                                    YhlogId = yhlog_id,
                                    Jcly = user.RoleType,
                                    CheckId = "jyplmhxtwbcwhyx",
                                    CheckName = "简易喷淋灭火系统未保持完好有效",
                                    CheckSm = from.ZmPcYh.JyplmhxtwbcwhyxMsg,
                                    CheckBy = userNumber,
                                    CheckTime = DateTime.Now,
                                    CheckZp = from.ZmPcYh.JyplmhxtwbcwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.JyplmhxtwbcwhyxPhoto.Split("|")),
                                    CreateOn = DateTime.Now,
                                    ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm,
                                    CheckNickname = user.Nickname
                                });
                                //如果整改
                                if (from.ZmPcYh.JyplmhxtwbcwhyxZgqk == 1)
                                {
                                    from.ZmPcYh.Pczt -= 1;
                                    if (string.IsNullOrWhiteSpace(from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto))
                                    {
                                        return new RespRet<int>(false, "简易喷淋灭火系统未保持完好有效整改照片不能为空");
                                    }
                                    listson.Add(new ZmPcYhlogDxzgsonCj
                                    {
                                        CheckId = "jyplmhxtwbcwhyx",
                                        CheckName = "简易喷淋灭火系统未保持完好有效",
                                        CreateOn = DateTime.Now,
                                        Fwbh = basemsg2.Fwbh,
                                        Id = Guid.NewGuid().ToString(),
                                        YhlogDxId = jyplhid,
                                        ZgBy = basemsg2.Hzxm,
                                        Zglx = "2",
                                        ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                        ZgZp = from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto.Split("|")),
                                        Xzq = basemsg2.Xzq,
                                        Xzqdm = basemsg2.Xzqdm,
                                        Town = basemsg2.Town,
                                        Towndm = basemsg2.Towndm,
                                        Village = basemsg2.Village,
                                        Villagedm = basemsg2.Villagedm
                                    }); ;
                                }
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "简易喷淋灭火系统未保持完好有效的值只能为1、0、2");
                    }

                    //参数验证
                    //烟感报警器未保持完好有效(1是0否2不涉及)
                    if (from.ZmPcYh.Ygbjwfwhyx == 1 || from.ZmPcYh.Ygbjwfwhyx == 0 || from.ZmPcYh.Ygbjwfwhyx == 2)
                    {
                        if (from.ZmPcYh.Ygbjwfwhyx == 2)
                        {

                        }
                        else
                        {
                            //如果等于1，描述不能为空
                            if (from.ZmPcYh.Ygbjwfwhyx == 1)
                            {
                                if (string.IsNullOrWhiteSpace(from.ZmPcYh.YgbjwfwhyxPhoto))
                                {
                                    return new RespRet<int>(false, "烟感报警器未保持完好有效的照片不能为空");
                                }
                                from.ZmPcYh.Pczt += 1;
                                //if (string.IsNullOrWhiteSpace(from.YgbjwfwhyxMsg))
                                //{
                                //    return new RespRet<int>(false, "烟感报警器未保持完好有效的描述不能为空");
                                //}
                                var ygbjid = Guid.NewGuid().ToString();
                                list.Add(new ZmPcYhLogDxCj
                                {
                                    Fwbh = from.ZmPcYh.Fwbh,
                                    Id = ygbjid,
                                    YhlogId = yhlog_id,
                                    Jcly = user.RoleType,
                                    CheckId = "ygbjwfwhyx",
                                    CheckName = "烟感报警器未保持完好有效",
                                    CheckSm = from.ZmPcYh.YgbjwfwhyxMsg,
                                    CheckBy = userNumber,
                                    CheckTime = DateTime.Now,
                                    CheckZp = from.ZmPcYh.YgbjwfwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.YgbjwfwhyxPhoto.Split("|")),
                                    CreateOn = DateTime.Now,
                                    ZgValue = from.ZmPcYh.UseElectricBicycleZgqk == null ? 0 : from.ZmPcYh.UseElectricBicycleZgqk,
                                    Xzq = basemsg2.Xzq,
                                    Xzqdm = basemsg2.Xzqdm,
                                    Town = basemsg2.Town,
                                    Towndm = basemsg2.Towndm,
                                    Village = basemsg2.Village,
                                    Villagedm = basemsg2.Villagedm,
                                    CheckNickname = user.Nickname
                                });
                                //如果整改
                                if (from.ZmPcYh.YgbjwfwhyxMsgZgqk == 1)
                                {
                                    from.ZmPcYh.Pczt -= 1;
                                    if (string.IsNullOrWhiteSpace(from.ZmPcYh.YgbjwfwhyxMsgZgPhoto))
                                    {
                                        return new RespRet<int>(false, "烟感报警器未保持完好有效整改照片不能为空");
                                    }
                                    listson.Add(new ZmPcYhlogDxzgsonCj
                                    {
                                        CheckId = "ygbjwfwhyx",
                                        CheckName = "烟感报警器未保持完好有效",
                                        CreateOn = DateTime.Now,
                                        Fwbh = basemsg2.Fwbh,
                                        Id = Guid.NewGuid().ToString(),
                                        YhlogDxId = ygbjid,
                                        ZgBy = basemsg2.Hzxm,
                                        Zglx = "2",
                                        ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                        ZgZp = from.ZmPcYh.YgbjwfwhyxMsgZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.YgbjwfwhyxMsgZgPhoto.Split("|")),
                                        Xzq = basemsg2.Xzq,
                                        Xzqdm = basemsg2.Xzqdm,
                                        Town = basemsg2.Town,
                                        Towndm = basemsg2.Towndm,
                                        Village = basemsg2.Village,
                                        Villagedm = basemsg2.Villagedm
                                    }); ;
                                }
                            }
                        }
                    }
                    else
                    {
                        return new RespRet<int>(false, "烟感报警器未保持完好有效的值只能为1、0、2");
                    }
                    if (string.IsNullOrWhiteSpace(from.ZmPcYh.Sign))
                    {
                        return new RespRet<int>(false, "签字信息不能为空");
                    }
                }







                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        //更新房屋状态表的状态----现在只有区级才能更改房屋状态，注释这段代码
                        #region
                        ////先获取到到街镇最新的一条记录
                        //var jz_log = _dbContext.ZmPcBaseSqjz.Where(c => c.Fwbh == from.ZmPcBase.Fwbh&&c.Yhly==4).FirstOrDefault();
                        ////获取状态表今日记录
                        //// 单行获取当天最后一刻并格式化
                        //string time = DateTime.Today
                        //    .AddHours(23)
                        //    .AddMinutes(59)
                        //    .AddSeconds(59)
                        //    .ToString("yyyy-MM-dd HH:mm:ss");
                        //string table_name = "zm_building_fwzt_" +DateTime.Now.Year;
                        //var fwzt_log=_dbContext.Database.GetDbConnection().Query<ZmBuildingFwzt>($" select *From {table_name} where fwbh=@fwbh and final_create_on=@time  ", new {@fwbh=from.ZmPcBase.Fwbh,@time=time}).FirstOrDefault();
                        ////如果没有记录，则应抛出异常
                        //if (fwzt_log == null)
                        //{
                        //    transaction.Rollback();
                        //    var r1 = new RespRet<int>(false, "操作失败");
                        //    WriteSystemLog("housescj/update-yhzc", r1, from);
                        //    return r1;
                        //}
                        ////找到记录，用当前村级状态与街镇最新状态判断出 最新的房屋状态，进行更新
                        //string update_sql = $" update {table_name} set final_fwzt=@fwzt where fwbh=@fwbh and final_create_on=@time  ";
                        ////如果街镇的记录为空，则直接使用村级的房屋状态
                        //if (jz_log == null)
                        //{
                        //    int? fwzt = from.ZmPcBase.Fwzt;
                        //    _dbContext.Database.GetDbConnection().Execute(update_sql, new {@fwbh= from.ZmPcBase.Fwbh, @time = time,@fwzt=fwzt });
                        //}
                        //else
                        //{
                        //    //优先使用本次的村级状态
                        //    if (from.ZmPcBase.Fwzt != null)
                        //    {
                        //        int? fwzt = from.ZmPcBase.Fwzt;
                        //        _dbContext.Database.GetDbConnection().Execute(update_sql, new { @fwbh = from.ZmPcBase.Fwbh, @time = time, @fwzt = fwzt });
                        //    }
                        //    else if (jz_log.Fwzt != null)
                        //    {
                        //        int? fwzt = jz_log.Fwzt;
                        //        _dbContext.Database.GetDbConnection().Execute(update_sql, new { @fwbh = from.ZmPcBase.Fwbh, @time = time, @fwzt = fwzt });
                        //    }
                        //    else
                        //    {
                        //        int? fwzt = null;
                        //        _dbContext.Database.GetDbConnection().Execute(update_sql, new { @fwbh = from.ZmPcBase.Fwbh, @time = time, @fwzt = fwzt });
                        //    }
                        //}
                        #endregion
                        //删除原来的村级
                        if (pc != null)
                        {
                            _dbContext.Remove(pc);
                            if (_dbContext.SaveChanges() == 0)
                            {
                                transaction.Rollback();
                                var r1 = new RespRet<int>(false, "操作失败");
                                WriteSystemLog("housescj/update-yhzc", r1, from);
                                return r1;
                            }
                            if (basemsg2.CreateOn >= Convert.ToDateTime("2024-04-01"))
                            {
                            }
                            else
                            {
                                if (DateTime.Now.Date >= Convert.ToDateTime("2024-04-01"))
                                {
                                    from.ZmPcBase.Ckfhwggptzlsl = pc.Ckfhwggptzlsl;
                                }
                            }
                        }
                        else
                        {
                            if (basemsg2.CreateOn >= Convert.ToDateTime("2024-04-01"))
                            {
                            }
                            else
                            {
                                if (DateTime.Now.Date >= Convert.ToDateTime("2024-04-01"))
                                {
                                    from.ZmPcBase.Ckfhwggptzlsl = 0;
                                }
                            }
                        }
                        if (from.ZmPcBase.Fwzt == 1 || from.ZmPcBase.Fwzt == 2)
                        {
                            if (pc != null)
                            {
                                int? fwzt = from.ZmPcBase.Fwzt;
                                from.ZmPcBase = pc;
                                from.ZmPcBase.Id = id;
                                from.ZmPcBase.Fwzt = fwzt;
                            }
                        }
                        if(from.ZmPcBase.Jsrysstd!="是"&& from.ZmPcBase.Jsrysstd != "否")
                        {
                            from.ZmPcBase.Jsrysstd = null;
                        }
                            _dbContext.Add(from.ZmPcBase);
                        int i = _dbContext.SaveChanges();
                        if (i > 0)
                        {
                            ZmPcBaseLogCj baselog = _mapper.Map<ZmPcBaseLogCj>(from.ZmPcBase);
                            baselog.Sfyzc = "是";
                            _dbContext.Add(baselog);
                            var res = _dbContext.SaveChanges();
                            if (res > 0)
                            {
                                //更新自查log表房屋状态，自查log表房屋状态已无用，2025-06-06注释掉
                                #region
                                //var zclog = _dbContext.ZmPcBaselog.Where(c => c.Fwbh == from.ZmPcBase.Fwbh).OrderByDescending(c => c.CreateOn).FirstOrDefault();
                                //if (zclog != null)
                                //{
                                //    if (from.ZmPcBase.Fwzt == null)
                                //    {
                                //        zclog.Fwzt = null;
                                //    }
                                //    else
                                //    {
                                //        zclog.Fwzt = from.ZmPcBase.Fwzt.ToString();
                                //    }
                                //    _dbContext.Update(zclog);
                                //    var zci = _dbContext.SaveChanges();
                                //    if (zci == 0)
                                //    {
                                //        transaction.Rollback();
                                //        var rerr = new RespRet<int>(false, "操作失败", zci);
                                //        WriteSystemLog("housescj/update-yhzc", rerr, from);
                                //        return rerr;
                                //    }
                                //}
                                #endregion
                                //如果房屋状态不正常，则往隐患表也放入一条数据
                                if (from.ZmPcBase.Fwzt == 1 || from.ZmPcBase.Fwzt == 2)
                                {           
                                    var relist = _dbContext.ZmPcYhCj.Where(c => c.Fwbh == from.ZmPcBase.Fwbh).ToList();
                                    //删除原来的zmpcyhcj表记录
                                    if (relist != null && relist.Count > 0)
                                    {
                                        _dbContext.RemoveRange(relist);
                                        if (_dbContext.SaveChanges() > 0)
                                        {
                                        }
                                        else
                                        {
                                            transaction.Rollback();
                                            var rerr = new RespRet<int>(false, "操作失败", i);
                                            WriteSystemLog("housescj/update-yhzc", rerr, from);
                                            return rerr;
                                        }
                                    }
                                    //添加默认数据zmpcyh
                                    #region
                                    ZmPcYhCj addcj2 = new ZmPcYhCj
                                    {
                                        BaseId = id,
                                        Id = Guid.NewGuid().ToString(),
                                        CreateBy = user.Number,
                                        CreateOn = DateTime.Now,
                                        CreateIp = this.IpAddress(),
                                        Fwbh = from.ZmPcBase.Fwbh,
                                        Jcly = user.RoleType,
                                        Pcsj = DateTime.Now,
                                        Pczt = 1,
                                        Xzqdm = basemsg2.Xzqdm,
                                        Town = basemsg2.Town,
                                        Village = basemsg2.Village,
                                        Xzq = basemsg2.Xzq,
                                        Towndm = basemsg2.Towndm,
                                        Villagedm = basemsg2.Villagedm
                                    };
                                    ZmPcYhLogCj addlogcj2 = _mapper.Map<ZmPcYhLogCj>(addcj2);
                                    _dbContext.Add(addcj2);
                                    _dbContext.Add(addlogcj2);
                                    if (_dbContext.SaveChanges() == 2)
                                    {
                                        transaction.Commit();
                                        return new RespRet<int>(true, "操作成功", i);
                                    }
                                    else
                                    {
                                        transaction.Rollback();
                                        var rerr = new RespRet<int>(false, "操作失败", i);
                                        WriteSystemLog("housescj/update-yhzc", rerr, from);
                                        return rerr;
                                    }
                                    #endregion
                                }
                                //隐患部分代码
                                var basemsg = _dbContext.ZmPcYhCj.AsNoTracking().FirstOrDefault(u => u.Fwbh == from.ZmPcYh.Fwbh && u.IsDelete != 1);
                                if (basemsg != null)
                                {
                                    _dbContext.Remove(basemsg);
                                    int remove_res = _dbContext.SaveChanges();
                                    if (remove_res == 0)
                                    {
                                        return new RespRet<int>(false, "操作失败");
                                    }
                                }
                                //之前没有采集过，设置默认信息
                                from.ZmPcYh.CreateBy = userNumber;
                                from.ZmPcYh.CreateOn = DateTime.Now;
                                from.ZmPcYh.CreateIp = this.IpAddress();
                                from.ZmPcYh.IsDelete = null;
                                from.ZmPcYh.DeleteBy = null;
                                from.ZmPcYh.DeleteOn = null;
                                from.ZmPcYh.DeleteIp = null;
                                from.ZmPcYh.Id = yhlog_id;
                                from.ZmPcYh.Fwbh = from.ZmPcYh.Fwbh;
                                from.ZmPcYh.Xzq = basemsg2.Xzq;
                                from.ZmPcYh.Xzqdm = basemsg2.Xzqdm;
                                from.ZmPcYh.Town = basemsg2.Town;
                                from.ZmPcYh.Towndm = basemsg2.Towndm;
                                from.ZmPcYh.Village = basemsg2.Village;
                                from.ZmPcYh.Villagedm = basemsg2.Villagedm;
                                from.ZmPcYh.Jcly = user.RoleType;
                                if (from.ZmPcYh.Pczt > 0)
                                {
                                    from.ZmPcYh.Pczt = 2;
                                }
                                else
                                {
                                    from.ZmPcYh.Pczt = 1;
                                }
                                var base1 = _dbContext.ZmPcBaseCj.AsNoTracking().FirstOrDefault(u => u.Fwbh == from.ZmPcYh.Fwbh && u.IsDelete != 1);
                                if (base1 != null)
                                {
                                    //通过双通道的值，判断是否需要加入隐患
                                    //if (base1.Jsrysstd == "否")
                                    //{
                                    //    var rysstdid = Guid.NewGuid().ToString();
                                    //    list.Add(new ZmPcYhLogDxCj
                                    //    {
                                    //        Fwbh = from.Fwbh,
                                    //        Id = rysstdid,
                                    //        YhlogId = yhlog_id,
                                    //        Jcly = zmUser.RoleType,
                                    //        CheckId = "wabzjsrysstd",
                                    //        CheckName = "未按照标准建设疏散通道",
                                    //        CheckSm = "未按照标准建设疏散通道",
                                    //        CheckBy = userNumber,
                                    //        CheckTime = DateTime.Now,
                              
                                    //        CreateOn = DateTime.Now,
                                    //        ZgValue = 0,
                                    //        Xzq = user.Xzq,
                                    //        Xzqdm = user.Xzqdm,
                                    //        Town = user.Town,
                                    //        Towndm = user.Towndm,
                                    //        Village = user.Village,
                                    //        Villagedm = user.Villagedm,
                                    //        CheckNickname = zmUser.Nickname
                                    //    });

                                    //}
                                    from.ZmPcYh.Pcsj = base1.Pcsj;
                                    from.ZmPcYh.BaseId = base1.Id;
                                }
                                else
                                {
                                    transaction.Rollback();
                                    return new RespRet<int>(false, "请先进行房屋基础信息采集", 0);
                                }
                                from.ZmPcYh.UseElectricBicyclePhoto = from.ZmPcYh.UseElectricBicyclePhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseElectricBicyclePhoto.Split("|"));
                                from.ZmPcYh.UseBottledLiquefiedGasPhoto = from.ZmPcYh.UseBottledLiquefiedGasPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseBottledLiquefiedGasPhoto.Split("|"));
                                from.ZmPcYh.UseStackedItemsPhoto = from.ZmPcYh.UseStackedItemsPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseStackedItemsPhoto.Split("|"));
                                from.ZmPcYh.SlljdxPhoto = from.ZmPcYh.SlljdxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.SlljdxPhoto.Split("|"));
                                from.ZmPcYh.YgbjwfwhyxPhoto = from.ZmPcYh.YgbjwfwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.YgbjwfwhyxPhoto.Split("|"));
                                from.ZmPcYh.MhqwbcwhyxPhoto = from.ZmPcYh.MhqwbcwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.MhqwbcwhyxPhoto.Split("|"));
                                from.ZmPcYh.JyplmhxtwbcwhyxPhoto = from.ZmPcYh.JyplmhxtwbcwhyxPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.JyplmhxtwbcwhyxPhoto.Split("|"));
                                from.ZmPcYh.UseElectricBicycleZgPhoto = from.ZmPcYh.UseElectricBicycleZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseElectricBicycleZgPhoto.Split("|"));
                                from.ZmPcYh.UseBottledLiquefiedGasZgPhoto = from.ZmPcYh.UseBottledLiquefiedGasZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseBottledLiquefiedGasZgPhoto.Split("|"));
                                from.ZmPcYh.UseStackedItemsZgPhoto = from.ZmPcYh.UseStackedItemsZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.UseStackedItemsZgPhoto.Split("|"));
                                from.ZmPcYh.SlljdxZgPhoto = from.ZmPcYh.SlljdxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.SlljdxZgPhoto.Split("|"));
                                from.ZmPcYh.YgbjwfwhyxMsgZgPhoto = from.ZmPcYh.YgbjwfwhyxMsgZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.YgbjwfwhyxMsgZgPhoto.Split("|"));
                                from.ZmPcYh.MhqwbcwhyxZgPhoto = from.ZmPcYh.MhqwbcwhyxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.MhqwbcwhyxZgPhoto.Split("|"));
                                from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto = from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto == null ? null : JsonConvert.SerializeObject(from.ZmPcYh.JyplmhxtwbcwhyxZgPhoto.Split("|"));

                               
                                _dbContext.Add(from.ZmPcYh);
                                int addres = _dbContext.SaveChanges();
                                if (addres > 0)
                                {
                                    //添加隐患单项及整改单项信息
                                    if (list != null && list.Count() > 0)
                                    {
                                        _dbContext.AddRange(list);
                                        if (_dbContext.SaveChanges() != list.Count())
                                        {
                                            transaction.Rollback();
                                            return new RespRet<int>(false, "操作失败", 0);
                                        }
                                    }
                                    if (listson != null && listson.Count() > 0)
                                    {
                                        _dbContext.AddRange(listson);
                                        if (_dbContext.SaveChanges() != listson.Count())
                                        {
                                            transaction.Rollback();
                                            return new RespRet<int>(false, "操作失败", 0);
                                        }
                                    }
                                    ZmPcYhLogCj baselog_cj = _mapper.Map<ZmPcYhLogCj>(from.ZmPcYh);
                                    baselog_cj.Id = yhlog_id;
                                    _dbContext.Add(baselog_cj);
                                    if (_dbContext.SaveChanges() > 0)
                                    {
                                        transaction.Commit();
                                        return new RespRet<int>(true, "操作成功", i);
                                    }
                                }
                                transaction.Rollback();
                                return new RespRet<int>(false, "操作失败", 0);
                            }
                        }
                        transaction.Rollback();
                        var r = new RespRet<int>(false, "操作失败", i);
                        WriteSystemLog("housescj/update-yhzc", r, from);
                        return r;
                    }
                    catch (Exception ex)
                    {
                        // 回滚事务
                        transaction.Rollback();
                        var res = new RespRet<int>(false, ex.ToString());
                        WriteSystemLogError("housescj/update-yhzc", res, from);
                        res.Msg = "系统异常";
                        return res;
                    }
                }
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("housescj/update-yhzc", res, from);
                res.Msg = "系统异常";
                return res;
            }
        }






        /// <summary>
        /// 修改用户姓名
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns>请求token</returns>
        //[AllowAnonymous]
        [HttpPost("housescj/updateusername")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<int> UpdateUserName([FromQuery] string name)
        {
            try
            {
                var userNumber = this.CurrentUser();
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<int>(false, "user not logined");
                }
                if (string.IsNullOrWhiteSpace(name))
                {
                    return new RespRet<int>(false, "用户姓名不能为空");
                }
                var user = _userRepository.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
                if (user == null)
                {
                    var res = new RespRet<int>(false, "未找到用户,请登录后重试");
                    WriteSystemLog("housescj/updatename", res, new { userNumber = userNumber, name = name });
                }
                user.Nickname = name;
                _dbContext.Update(user);
                int i = _dbContext.SaveChanges();
                if (i > 0)
                {
                    var res = new RespRet<int>(true, "修改成功", i);
                    WriteSystemLog("housescj/updatename", res, new { userNumber = userNumber, name = name });
                    return res;
                }
                else
                {
                    var res = new RespRet<int>(false, "修改失败", i);
                    WriteSystemLog("housescj/updatename", res, new { userNumber = userNumber, name = name });
                    return res;
                }
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("login/confirm", res, name);
                res.Msg = "修改失败";
                return res;
            }
        }


        /// <summary>
        /// 获取技术支持电话
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns>请求token</returns>
        [AllowAnonymous]
        [HttpPost("housescj/getphone")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<object> getphone()
        {
            try
            {
                var userNumber = this.CurrentUser();   
                var user = _userRepository.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
                if (user == null)
                {
                    var res = new RespRet<int>(false, "未找到用户,请登录后重试");
                    WriteSystemLog("housescj/getphone", res, new { userNumber = userNumber});
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(" select id,name,phone From zm_help_phone where type='1' order by idx ").ToList();
                var datas = _dbContext.Database.GetDbConnection().Query<object>(" select id,name,phone From zm_help_phone where type='2' order by idx ").ToList();
                return new RespRet<object>(true, "获取成功", new {xfdata=data,sysdata=datas });
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/getphone", res,null);
                res.Msg = "获取失败";
                return res;
            }
        }



        /// <summary>
        /// 修改用户姓名
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns>请求token</returns>
        //[AllowAnonymous]
        [HttpPost("housescj/updateunitname")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<int> UpdateUnitName([FromQuery] string name)
        {
            try
            {
                var userNumber = this.CurrentUser();
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<int>(false, "user not logined");
                }
                if (string.IsNullOrWhiteSpace(name))
                {
                    return new RespRet<int>(false, "单位不能为空");
                }
                var user = _userRepository.FirstOrDefault(c => c.Number == userNumber && c.Disabled != 1);
                if (user == null)
                {
                    var res = new RespRet<int>(false, "未找到用户,请登录后重试");
                    WriteSystemLog("housescj/updateunitname", res, new { userNumber = userNumber, name = name });
                }
                user.Unitname = name;
                _dbContext.Update(user);
                int i = _dbContext.SaveChanges();
                if (i > 0)
                {
                    var res = new RespRet<int>(true, "修改成功", i);
                    WriteSystemLog("housescj/updateunitname", res, new { userNumber = userNumber, name = name });
                    return res;
                }
                else
                {
                    var res = new RespRet<int>(false, "修改失败", i);
                    WriteSystemLog("housescj/updateunitname", res, new { userNumber = userNumber, name = name });
                    return res;
                }
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("housescj/updateunitname", res, name);
                res.Msg = "修改失败";
                return res;
            }
        }




        /// <summary>
        /// 获取隐患整改详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/search-gjzz")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcGjzzCj>), 200)]
        public RespRet<ZmPcGjzzCj> Yhxq2([FromQuery] string villagedm)
        {
            var userNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZmPcGjzzCj>(false, "user not logined");
                }
                if (string.IsNullOrEmpty(villagedm))
                {
                    return new RespRet<ZmPcGjzzCj>(false, "村代码不能为空");
                }
                var from = _dbContext.ZmPcGjzzCj.FirstOrDefault(c => c.Villagedm == villagedm);
                if (from == null)
                {
                    from = new ZmPcGjzzCj();
                }
                from.Fj = from.Fj == null ? null : string.Join("|", JsonStr2List(from.Fj));
                //获取统计信息
                string sql = @"select 1 id, mzltlj,zzfasl,zzsgsl,
case when qcddzxcsl is null then 0 else qcddzxcsl end qcddzxcsl,
case when sjjzrksl is null then 0 else sjjzrksl end sjjzrksl,
case when azgdjsbpsl is null then 0 else azgdjsbpsl end azgdjsbpsl,
case when qlddzxcsl is null then 0 else qlddzxcsl end qlddzxcsl,
case when qlyhsyqpsl is null then 0 else qlyhsyqpsl end qlyhsyqpsl
from (select count(case when jzxyssltsl>=2 then 1 end) mzltlj,
count(case when sstdjsqk='正在制定方案' then 1 end) zzfasl,
count(case when sstdjsqk='正在施工改造' then 1 end)zzsgsl,
sum(bdfwjzrydddzxcsl)  qcddzxcsl,
sum(jzrks) sjjzrksl,
sum(xggdjsbp) azgdjsbpsl
from zm_pc_base where village=@villagedm) a,
(select sum(zxqlddcsl)qlddzxcsl,sum(zxqlyhsyqsl) qlyhsyqpsl  from zm_pc_yhlog a left join zm_buildings b on  a.fwbh=b.fwbh where b.villagedm=@villagedm) b
";
                var sqlParams = new List<NpgsqlParameter>();
                sqlParams.Add(new NpgsqlParameter("villagedm", $"{villagedm}"));
                var tj = _dbContext.ZmPcGjzzCjTj.FromSqlRaw(sql.ToString(), sqlParams.ToArray()).FirstOrDefault();
                if (tj != null)
                {
                    from.Mzltlj = tj.Mzltlj;
                    from.Zzfasl = tj.Zzfasl;
                    from.Zzsgsl = tj.Zzsgsl;
                    from.Qcddzxcsl = tj.Qcddzxcsl;
                    from.Sjjzrksl = tj.Sjjzrksl;
                    from.Azgdjsbpsl = tj.Azgdjsbpsl;
                    from.Qlddzxcsl = tj.Qlddzxcsl;
                    from.Qlyhsyqpsl = tj.Qlyhsyqpsl;
                }
                var r = new RespRet<ZmPcGjzzCj>(true, "Ok", from);
                WriteSystemLog("housescj/search-gjzz", r, villagedm);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZmPcGjzzCj>(false, ex.ToString());
                WriteSystemLogError("housescj/search-gjzz", res, villagedm);
                res.Msg = "系统异常";
                return res;
            }
        }








        /// <summary>
        /// 攻坚整治填报-村级
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/update-gjzztb")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<int>), 200)]
        public RespRet<int> Gjzztb([FromBody] ZmPcGjzzCjDto model)
        {
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<int>(false, "user not logined");
            }
            try
            {
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                if (RegionCode != model.Villagedm)
                {
                    var r1 = new RespRet<int>(false, "请传递正确的村代码");
                    WriteSystemLog("housescj/update-gjzztb", r1, model);
                    return r1;
                }
                var pc = _dbContext.ZmPcGjzzCj.AsNoTracking().FirstOrDefault(u => u.Villagedm == model.Villagedm);
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        if (pc != null)
                        {
                            _dbContext.Remove(pc);
                            if (_dbContext.SaveChanges() == 0)
                            {
                                transaction.Rollback();
                                var r1 = new RespRet<int>(false, "操作失败");
                                WriteSystemLog("housescj/update-gjzztb", r1, model);
                                return r1;
                            }
                        }
                        model = _mapper.Map<ZmPcGjzzCjDto>(model);
                        var add = _mapper.Map<ZmPcGjzzCj>(model);
                        string sql = " select * from zm_building_ledger where villagedm=@villagedm ";
                        var region = _dbContext.ZmBuildingLedger.AsNoTracking().FirstOrDefault(u => u.villagedm == model.Villagedm);
                        if (region == null)
                        {
                            transaction.Rollback();
                            var r1 = new RespRet<int>(false, "未找到村庄，请检查村庄代码");
                            WriteSystemLog("housescj/update-gjzztb", r1, model);
                            return r1;
                        }
                        add.Xzq = region.xzq;
                        add.Xzqdm = region.xzqdm;
                        add.Town = region.town;
                        add.Towndm = region.towndm;
                        add.Village = region.village;
                        add.Villagedm = region.villagedm;
                        add.CreateBy = user.Number;
                        add.CreateName = user.Nickname;
                        add.Pcsj = DateTime.Now.ToString();
                        add.CreateOn = DateTime.Now;
                        add.Fj = string.IsNullOrWhiteSpace(model.Fj) ? null : JsonConvert.SerializeObject(add.Fj.Split("|"));
                        _dbContext.Add(add);
                        int i = _dbContext.SaveChanges();
                        if (i > 0)
                        {
                            ZmPcGjzzCjLog log = _mapper.Map<ZmPcGjzzCjLog>(add);
                            _dbContext.Add(log);
                            var res = _dbContext.SaveChanges();
                            if (res > 0)
                            {
                                transaction.Commit();
                                var r1 = new RespRet<int>(true, "操作成功", i);
                                WriteSystemLog("housescj/update-gjzztb", r1, model);
                                return r1;
                            }
                        }
                        transaction.Rollback();
                        var r = new RespRet<int>(false, "操作失败", i);
                        WriteSystemLog("housescj/update-gjzztb", r, model);
                        return r;
                    }
                    catch (Exception ex)
                    {
                        // 回滚事务
                        transaction.Rollback();
                        var res = new RespRet<int>(false, ex.ToString());
                        WriteSystemLogError("housescj/update-gjzztb", res, model);
                        res.Msg = "系统异常";
                        return res;
                    }
                }
            }
            catch (Exception ex)
            {
                var res = new RespRet<int>(false, ex.ToString());
                WriteSystemLogError("housescj/update-gjzztb", res, model);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 获取所有村庄
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("housescj/search-villagelist")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcGjzzCj>), 200)]
        public RespRet<List<ZmVillage>> GetVillageList([FromQuery] string village)
        {
            try
            {

                string sql = @"select a.region_name village,b.region_name town,c.region_name xzq,a.region_code villagedm from zm_department a,zm_department b,
zm_department c where a.regional_level='7' and b.regional_level='6' and c.regional_level='5' and a.parent_id=b.region_code and b.parent_id=c.region_code and a.region_name like @village  ORDER BY a.region_code";
                var sqlParams = new List<NpgsqlParameter>();
                sqlParams.Add(new NpgsqlParameter("village", $"{"%" + village + "%"}"));
                var villagelist = _dbContext.ZmVillage.FromSqlRaw(sql.ToString(), sqlParams.ToArray()).ToList();

                var r = new RespRet<List<ZmVillage>>(true, "Ok", villagelist);
                WriteSystemLog("housescj/search-villagelist", r, village);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<List<ZmVillage>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-villagelist", res, village);
                res.Msg = "系统异常";
                return res;
            }
        }






        private byte[] GetDynamicVectorTile(int z, int x, int y)
        {
            var sql = @$"SELECT 
ST_AsMVT(P, 'ffpolygon', 4096, 'geom') AS tile 
FROM 
(SELECT 
ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( {z}, {x}, {y}),4096,256,FALSE) AS geom, 
id,fwbh,yhfwbh,address
FROM zm_buildings_pc 
WHERE ST_Intersects(ST_SetSRID(geom, 4326), ST_Transform(ST_TileEnvelope( {z}, {x}, {y}), 4326))) AS P";
            var tile = _dbContext.Database.GetDbConnection().Query<byte[]>(sql).FirstOrDefault();
            return tile;
        }

        private byte[] GetDynamicVectorTile__build(int z, int x, int y)
        {





            var userNumber = this.CurrentUser();

            if (string.IsNullOrEmpty(userNumber))
            {
                return null;
            }
            var sql = "";
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1 && u.Disabled != 1);
            var roleType = (PermissionRoleType)user.RoleType;
            var codeWhere = Utilitys.GetLikeSqlWhere_yt(user.Jurisdiction, null);
            if (roleType == PermissionRoleType.Admin || string.IsNullOrEmpty(codeWhere))
            {
                sql = @$"SELECT 
                ST_AsMVT(P, 'ffpolygon', 4096, 'geom') AS tile 
                FROM 
                (SELECT 
                ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( {z}, {x}, {y}),4096,256,FALSE) AS geom, 
                ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( 17, 107880, 49659),4096,256,FALSE) AS geom, 
                 zb.build_no,zb.build_name,zb.district_id,zb.district_name,zb.street_name,zb.street_id2021,zb.jcnd from  zm_yt_build  zb, zm_yt_buildmap zbm
                where zb.build_no =zbm.pfhouseid and ST_Intersects(ST_SetSRID(geom, 4326), ST_Transform(ST_TileEnvelope( {z}, {x}, {y}), 4326))) AS P";

            }
            else
            {

                if (!string.IsNullOrEmpty(codeWhere))
                {
                    sql = @$"SELECT 
                ST_AsMVT(P, 'ffpolygon', 4096, 'geom') AS tile 
                FROM 
                (SELECT 
                ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( {z}, {x}, {y}),4096,256,FALSE) AS geom, 
                ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( 17, 107880, 49659),4096,256,FALSE) AS geom, 
                 zb.build_no,zb.build_name,zb.district_id,zb.district_name,zb.street_name,zb.street_id2021,zb.jcnd from  zm_yt_build  zb, zm_yt_buildmap zbm
                where " + codeWhere + @$" and  zb.build_no =zbm.pfhouseid and ST_Intersects(ST_SetSRID(geom, 4326), ST_Transform(ST_TileEnvelope( {z}, {x}, {y}), 4326))) AS P";

                }

            }

            var tile = _dbContext.Database.GetDbConnection().Query<byte[]>(sql).FirstOrDefault();
            return tile;



        }


        private byte[] GetRiskHouseDynamicVectorTile(int z, int x, int y)
        {
            var sql = @$"SELECT 
ST_AsMVT(P, 'ffpolygon', 4096, 'geom') AS tile 
FROM 
(SELECT 
ST_AsMVTGeom( ST_Transform(ST_SetSRID(geom, 4326), 3857), ST_TileEnvelope( {z}, {x}, {y}),4096,256,FALSE) AS geom,fwyt, 
id,fwbh,yhfwbh,address,result,city_result as cityResult,aqyhcp 
FROM risk_census_house_yhpc 
WHERE ST_Intersects(ST_SetSRID(geom, 4326), ST_Transform(ST_TileEnvelope( {z}, {x}, {y}), 4326))) AS P";
            var tile = _dbContext.Database.GetDbConnection().Query<byte[]>(sql).FirstOrDefault();
            return tile;
        }













        /// <summary>
        /// ks3 upload
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("uploadks3")]
        [HttpPost("zm_buildings/upload")]
        public async Task<IActionResult> Upload(IFormFile file)
        {
            try
            {
                if (file == null || file.Length == 0)
                    return BadRequest("Invalid file");
                // 保存文件到服务器
                var extension = System.IO.Path.GetExtension(file.FileName).ToLower();

                string BucketName = "xfpc-test";

                string path = "";
                FileInfo fileInfo = ConvertFormFileToFileInfo(file, out path);
                string key = Guid.NewGuid().ToString() + extension;
                CannedAccessControlList cannedAcl = new CannedAccessControlList(CannedAccessControlList.PUBLICK_READ_WRITE);
                // 上传
                var result = _kS3Client.putObject(BucketName, key, fileInfo);
                // 成功后设置对象为公共读写
                if (!string.IsNullOrEmpty(result.getContentMD5()))
                {
                    _kS3Client.setObjectAcl(BucketName, key, cannedAcl);
                }
                var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                     .AddJsonFile("appsettings.json", optional: true).Build();
                var logEnable = configuration.GetSection("ks3url");
                System.IO.File.Delete(path);
                key = logEnable.Value + key;
                //string rkey = "https://xfpc-test.ks3-cn-beijing.ksyuncs.com/" + extension;
                // 返回文件路径
                return Ok(new { imagePath = key });
            }
            catch (Exception ex)
            {
                WriteSystemLogError("zm_buildings/upload", new RespRet(false, ex.ToString()), null);
                return BadRequest("上传失败");
            }
        }
        private FileInfo ConvertFormFileToFileInfo(IFormFile formFile, out string path)
        {
            // 创建一个临时文件路径
            string tempFilePath = System.IO.Path.GetTempFileName();

            // 使用 IFormFile 对象的 OpenReadStream() 方法来获取文件流
            using (var stream = new FileStream(tempFilePath, FileMode.Create))
            {
                formFile.CopyTo(stream);
            }

            // 创建 FileInfo 对象
            FileInfo fileInfo = new FileInfo(tempFilePath);
            path = fileInfo.FullName;
            return fileInfo;
        }









        [HttpGet("getfile")]
        public async Task<IActionResult> getfile(string key)
        {
            var file = _kS3Client.getObject("xfpc-test", key);//"c1d276ab-f3a8-4936-9aab-d6a12c77a03c");
            var ObjectContent = file.getObjectContent();
            StreamReader sr = new StreamReader(ObjectContent);
            var ObjectMetadata = file.getObjectMetadata();
            //StreamReader sr = new StreamReader(file.getObjectContent());
            var filebyte = ConvertStreamReaderToByteArray(sr);

            string img = Convert.ToBase64String(filebyte);
            // 返回文件路径
            //return File(filebyte, "image/png", key );
            return Ok(new { imagePath = img });
        }
        //[HttpGet("getfile")]
        //public async Task<IActionResult> getfile(string key)
        //{
        //    // 获取后缀名
        //    var extension = Path.GetExtension(key).ToLower();
        //    // 获取mimetype
        //    var mine = "image/" + extension.Substring(1, extension.Length - 1);
        //    var file = _kS3Client.getObject("xfpc-test", key);
        //    // 把Content转成byte返回
        //    var filebyte = file.getObjectContent().ReadAsBytes();
        //    // 返回文件路径
        //    return File(filebyte, mine, key);
        //}

        private byte[] ConvertStreamReaderToByteArray(StreamReader reader)
        {
            // 使用 MemoryStream 来存储字节数据
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // 将 StreamReader 的内容复制到 MemoryStream 中
                using (StreamWriter streamWriter = new StreamWriter(memoryStream))
                {
                    streamWriter.Write(reader.ReadToEnd());
                    streamWriter.Flush();
                }

                // 将 MemoryStream 转换为字节数组
                byte[] byteArray = memoryStream.ToArray();

                return byteArray;
            }
        }

        [HttpGet, Route("getfilelistks3")]
        public async Task<IActionResult> getfilelist()
        {
            var filelist = _kS3Client.listObjects("xfpc-test");//"c1d276ab-f3a8-4936-9aab-d6a12c77a03c");
            var kkk = filelist.getObjectSummaries().ToList();
            return Ok(new RespRet<List<ObjectSummary>>(true, "Ok", kkk));
        }


    }
}